admin管理员组文章数量:1580452
在浏览器的兼容性测试中,会测试产品在不同浏览器上的兼容性,比较主流的浏览器有IE、Firefox、Chrome,Opera,Safari等。还有其它如360、QQ、遨游、百度等浏览器都是基于IE或者chrome内核,或者IE+Chrome双内核开发,在测试这类浏览器时可以调用对应内核驱动。
不同的浏览器需要对应的驱动程序,这样selenium才能与浏览器进行通信。在启动WebDriver之前可以指定驱动的绝对位置,但还是建议将驱动添加到环境变量中,这样代码更易于维护,容易移植。
下面介绍selenium对几种浏览器的远程控制方法:
目录
- Chrome浏览器
- 1. 下载驱动
- 2. python代码实现
- Firefox-火狐浏览器
- 1. 下载驱动
- 2. python代码
- IE浏览器
- 1. 下载驱动
- 2. python代码
- Edge浏览器
- 1. 下载驱动
- 2. python代码
- Opera浏览器-欧朋浏览器
- 1. 下载驱动
- 2. python代码
- 其它浏览器
- 360极速浏览器
- 2345浏览器
- 浏览器不同语言
- 检查浏览器语言
- Chrome浏览器
- 火狐浏览器
- Opera浏览器
- 系列文章
Chrome浏览器
1. 下载驱动
首先查看浏览器版本号,根据浏览器的版本号去下载对应的 chromedriver,Chrome浏览器版本与对应的驱动参考:https://sites.google/a/chromium/chromedriver/downloads
驱动下载地址:https://chromedriver.storage.googleapis/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')
Firefox-火狐浏览器
1. 下载驱动
浏览器版本、驱动geckodriver版本、Selenium版本对应关系参考:https://firefox-source-docs.mozilla/testing/geckodriver/Support.html
浏览器下载地址:http://ftp.mozilla/pub/firefox/releases/
驱动下载地址:https://github/mozilla/geckodriver/releases
2. python代码
from selenium.webdriver import Firefox
self.driver = webdriver.Firefox()
self.driver.get('https://www.baidu')
IE浏览器
1. 下载驱动
下载IEDriverServer.exe :http://selenium-release.storage.googleapis/index.html
IE浏览器下载:https://support.microsoft/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')
Edge浏览器
1. 下载驱动
Edge浏览器版本与edgedriver驱动版本对应关系参考:https://developer.microsoft/en-us/microsoft-edge/tools/webdriver/
edgedriver驱动下载地址:https://msedgewebdriverstorage.z22.web.core.windows/
2. python代码
from selenium.webdriver import Edge
self.driver = Edge()
self.driver.get('https://www.baidu')
Opera浏览器-欧朋浏览器
1. 下载驱动
Opera浏览器版本与OperaDriver驱动版本对应关系参考:https://github/operasoftware/operachromiumdriver/releases
Opera浏览器历史版本下载地址:https://get.geo.opera/pub/opera/desktop/
2. python代码
from selenium.webdriver import Opera
self.driver = Opera()
self.driver.get('https://www.baidu')
其它浏览器
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')
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')
其它基于chrome内核的浏览器也可以使用这种方法来驱动。
浏览器不同语言
检查浏览器语言
可以通过使用selenium执行JavaScript语句来获取浏览器语言
lang = self.driver.execute_script("return window.navigator.language;")
在浏览器的控制台执行:
> window.navigator.language;
'zh-CN'
Chrome浏览器
可以以不同语言拉起Chrome浏览器,测试产品的语言自适应功能。
options = Options()
lang = "zh-CN"
options.add_argument("--lang={}".format(lang))
self.driver = webdriver.Chrome(chrome_options=options)
self.driver.get('https://www.baidu')
不同国家的语言代码可以参考:https://developers.google/admin-sdk/directory/v1/languages
或者
打开火狐浏览器语言设置界面查看各个国家的语言代码:
火狐浏览器
火狐浏览器也可以实现这个功能,实现方式如下(在我电脑上没有成功,可能版本问题):
profile = webdriver.FirefoxProfile()
profile.set_preference('intl.accept_languages', 'fr')
self.driver = webdriver.Firefox(firefox_profile=profile)
self.driver.get('https://www.baidu')
Opera浏览器
Opera浏览器实现方式:
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')
IE浏览器语言设置需要设置系统语言,需要系统重启,无法通过设置参数的方式进行自动化设置语言。另外还有一种方案是可以通过自动化安装不同语言版本的浏览器进行测试。
系列文章
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执行JavaScript脚本
10、selenium/appium 等待方式介绍
11、Selenium Grid:在多个主机上并行执行自动化脚本
欢迎关注公众号:「测试开发小记」及时接收最新技术文章!
版权声明:本文标题:Selenium多浏览器测试 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1727865646a1134419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论