如何使用代理啟動 Selenium 瀏覽器?
我們可以使用代理啟動 Selenium 瀏覽器。代理伺服器是執行本地化測試的重要工具。我們可以拿一個電子商務網站來驗證顯示的語言和貨幣是否符合使用者所在的位置。
在測試中使用代理伺服器,我們可以測試特定區域使用者的網站外觀和感覺。首先,我們必須使用以下步驟來配置經過身份驗證的代理伺服器 −
從 Selenium 程式包中匯入 webdriver。
宣告代理伺服器。
配置 ChromeOptions 類
將代理伺服器與 ChromeOptions 繫結。
將選項傳遞給 Chrome() 物件。
示例
配置代理伺服器的程式碼實現。
from selenium import webdriver #proxy server definition p = "127.20.0.0:8080" #configure ChromeOptions class chrome_options = WebDriverWait.ChromeOptions() #adding proxy parameter to options chrome_options.add_argument('--proxy-server=%s' % p) #adding options to Chrome driver = webdriver.Chrome(chrome_options= chrome_options) driver.implicitly_wait(0.5) driver.get("https://tutorialspoint.tw/index.htm")
接下來,為了驗證搜尋欄位是否會自動顯示當前使用者地址,我們將使用以下程式碼片段 −
def checkLocation(self): self.driver.get(self.url) s = self.driver.find_element_by_name('location') #verify location with assertion self.assertEqual('USA', s.text)
如果我們必須檢查多個位置,我們可以建立一個方法並將代理 IP 地址作為引數傳遞。
廣告