Selenium多浏览器测试
在瀏覽器的兼容性測試中,會測試產(chǎn)品在不同瀏覽器上的兼容性,比較主流的瀏覽器有IE、Firefox、Chrome,Opera,Safari等。還有其它如360、QQ、遨游、百度等瀏覽器都是基于IE或者chrome內(nèi)核,或者IE+Chrome雙內(nèi)核開發(fā),在測試這類瀏覽器時可以調(diào)用對應內(nèi)核驅動。
不同的瀏覽器需要對應的驅動程序,這樣selenium才能與瀏覽器進行通信。在啟動WebDriver之前可以指定驅動的絕對位置,但還是建議將驅動添加到環(huán)境變量中,這樣代碼更易于維護,容易移植。
下面介紹selenium對幾種瀏覽器的遠程控制方法:
目錄
- Chrome瀏覽器
- 1. 下載驅動
- 2. python代碼實現(xiàn)
- Firefox-火狐瀏覽器
- 1. 下載驅動
- 2. python代碼
- IE瀏覽器
- 1. 下載驅動
- 2. python代碼
- Edge瀏覽器
- 1. 下載驅動
- 2. python代碼
- Opera瀏覽器-歐朋瀏覽器
- 1. 下載驅動
- 2. python代碼
- 其它瀏覽器
- 360極速瀏覽器
- 2345瀏覽器
- 瀏覽器不同語言
- 檢查瀏覽器語言
- Chrome瀏覽器
- 火狐瀏覽器
- Opera瀏覽器
- 系列文章
Chrome瀏覽器
1. 下載驅動
首先查看瀏覽器版本號,根據(jù)瀏覽器的版本號去下載對應的 chromedriver,Chrome瀏覽器版本與對應的驅動參考:https://sites.google.com/a/chromium.org/chromedriver/downloads
驅動下載地址:https://chromedriver.storage.googleapis.com/index.html
解壓并將驅動添加到環(huán)境變量中
2. python代碼實現(xiàn)
from selenium.webdriver import Chrome browser_locale = 'fr-FR' options = Options() options.add_argument("--lang={}".format(browser_locale)) # 設置瀏覽器語言 self.driver = webdriver.Chrome(chrome_options=options) self.driver.get('https://www.baidu.com')Firefox-火狐瀏覽器
1. 下載驅動
瀏覽器版本、驅動geckodriver版本、Selenium版本對應關系參考:https://firefox-source-docs.mozilla.org/testing/geckodriver/Support.html
瀏覽器下載地址:http://ftp.mozilla.org/pub/firefox/releases/
驅動下載地址:https://github.com/mozilla/geckodriver/releases
2. python代碼
from selenium.webdriver import Firefox self.driver = webdriver.Firefox() self.driver.get('https://www.baidu.com')IE瀏覽器
1. 下載驅動
下載IEDriverServer.exe :http://selenium-release.storage.googleapis.com/index.html
IE瀏覽器下載:https://support.microsoft.com/zh-cn/topic/%E4%B8%8B%E8%BD%BD-internet-explorer-11-%E8%84%B1%E6%9C%BA%E5%AE%89%E8%A3%85%E7%A8%8B%E5%BA%8F-99d492a1-3a62-077b-c476-cf028aff9a7f
注意:設置internet選項>安全 這4個選項全勾選或者不勾選,不然無法驅動IE瀏覽器。
2. python代碼
from selenium.webdriver import Ie self.driver = webdriver.Ie() self.driver.get('https://www.baidu.com')Edge瀏覽器
1. 下載驅動
Edge瀏覽器版本與edgedriver驅動版本對應關系參考:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
edgedriver驅動下載地址:https://msedgewebdriverstorage.z22.web.core.windows.net/
2. python代碼
from selenium.webdriver import Edge self.driver = Edge() self.driver.get('https://www.baidu.com')Opera瀏覽器-歐朋瀏覽器
1. 下載驅動
Opera瀏覽器版本與OperaDriver驅動版本對應關系參考:https://github.com/operasoftware/operachromiumdriver/releases
Opera瀏覽器歷史版本下載地址:https://get.geo.opera.com/pub/opera/desktop/
2. python代碼
from selenium.webdriver import Opera self.driver = Opera() self.driver.get('https://www.baidu.com')其它瀏覽器
360極速瀏覽器
360極速瀏覽器采用chrome內(nèi)核,可以使用對應版本的chromedriver
option=webdriver.ChromeOptions() option.binary_location=r'D:/software/360Chrome/Chrome/Application/360chrome.exe' self.driver=webdriver.Chrome(options=option) self.driver.get('https://www.baidu.com')binary_location為360極速瀏覽器安裝路徑下的可執(zhí)行文件360chrome.exe的路徑
2345瀏覽器
2345瀏覽器是基于IE+Chrome雙內(nèi)核開發(fā),可以使用chromedriver來驅動它:
option=webdriver.ChromeOptions() option.binary_location=r'C:/Program Files (x86)/2345Soft/2345Explorer/2345Explorer.exe' self.driver=webdriver.Chrome(options=option) self.driver.get('https://www.baidu.com')其它基于chrome內(nèi)核的瀏覽器也可以使用這種方法來驅動。
瀏覽器不同語言
檢查瀏覽器語言
可以通過使用selenium執(zhí)行JavaScript語句來獲取瀏覽器語言
lang = self.driver.execute_script("return window.navigator.language;")在瀏覽器的控制臺執(zhí)行:
> window.navigator.language; 'zh-CN'Chrome瀏覽器
可以以不同語言拉起Chrome瀏覽器,測試產(chǎn)品的語言自適應功能。
options = Options() lang = "zh-CN" options.add_argument("--lang={}".format(lang)) self.driver = webdriver.Chrome(chrome_options=options) self.driver.get('https://www.baidu.com')不同國家的語言代碼可以參考:https://developers.google.com/admin-sdk/directory/v1/languages
或者
打開火狐瀏覽器語言設置界面查看各個國家的語言代碼:
火狐瀏覽器
火狐瀏覽器也可以實現(xiàn)這個功能,實現(xiàn)方式如下(在我電腦上沒有成功,可能版本問題):
profile = webdriver.FirefoxProfile() profile.set_preference('intl.accept_languages', 'fr') self.driver = webdriver.Firefox(firefox_profile=profile) self.driver.get('https://www.baidu.com')Opera瀏覽器
Opera瀏覽器實現(xiàn)方式:
browser_locale = 'en-US' options = Options() options.add_argument("--lang={}".format(browser_locale)) self.driver = webdriver.Opera(options=options) self.driver.get('https://www.baidu.com')IE瀏覽器語言設置需要設置系統(tǒng)語言,需要系統(tǒng)重啟,無法通過設置參數(shù)的方式進行自動化設置語言。另外還有一種方案是可以通過自動化安裝不同語言版本的瀏覽器進行測試。
--THE END--系列文章
1、Selenium Webdriver 架構
2、Selenium Web元素定位方法
3、Selenium Web元素操作
4、Web自動化測試:xpath & CSS Selector定位
5、Selenium ActionChains、TouchAction方法
6、Selenium switch_to方法
7、Selenium Select下拉框
8、Selenium多瀏覽器測試
9、Selenium執(zhí)行JavaScript腳本
10、selenium/appium 等待方式介紹
11、Selenium Grid:在多個主機上并行執(zhí)行自動化腳本
歡迎關注公眾號:「測試開發(fā)小記」及時接收最新技術文章!
總結
以上是生活随笔為你收集整理的Selenium多浏览器测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [乐意黎原创] 删除QQ的MiniBro
- 下一篇: 使用 C 语言打开浏览器