天天看點

Selenium多浏覽器測試

在浏覽器的相容性測試中,會測試産品在不同浏覽器上的相容性,比較主流的浏覽器有IE、Firefox、Chrome,Opera,Safari等。還有其它如360、QQ、遨遊、百度等浏覽器都是基于IE或者chrome核心,或者IE+Chrome雙核心開發,在測試這類浏覽器時可以調用對應核心驅動。

不同的浏覽器需要對應的驅動程式,這樣selenium才能與浏覽器進行通信。在啟動WebDriver之前可以指定驅動的絕對位置,但還是建議将驅動添加到環境變量中,這樣代碼更易于維護,容易移植。

下面介紹selenium對幾種浏覽器的遠端控制方法:

Chrome浏覽器

1. 下載下傳驅動

首先檢視浏覽器版本号,根據浏覽器的版本号去下載下傳對應的 chromedriver,Chrome浏覽器版本與對應的驅動參考:https://sites.google.com/a/chromium.org/chromedriver/downloads

驅動下載下傳位址:https://chromedriver.storage.googleapis.com/index.html

解壓并将驅動添加到環境變量中

2. python代碼實作

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/

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浏覽器。

Selenium多浏覽器測試

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核心,可以使用對應版本的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極速浏覽器安裝路徑下的可執行檔案360chrome.exe的路徑

2345浏覽器

2345浏覽器是基于IE+Chrome雙核心開發,可以使用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核心的浏覽器也可以使用這種方法來驅動,基于IE核心開發的浏覽器還沒辦法控制。

--THE END--

文章标題:Selenium多浏覽器測試

本文作者:hiyo

本文連結:https://hiyong.gitee.io/posts/selenium-browsers/

歡迎關注公衆号:「測試開發小記」及時接收最新技術文章!