selenium兼容非标准chrome内核的浏览器
Posted hupilan521
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium兼容非标准chrome内核的浏览器相关的知识,希望对你有一定的参考价值。
多浏览器兼容性测试(1)
RIDE已经支持多浏览器兼容性测试,例如:
firefox
ie
chrome
safari
但是,项目要求支持360极速和360安全浏览器。所以,我们需要增加代码让RIDE识别。其他浏览器类似(本地浏览器)。
说明:基于Selenium: 3.0.2
from selenium.webdriver.chrome.options import Options from selenium import webdriver from selenium.webdriver.common.keys import Keys import time __browser_url = r‘C:UsersAdministratorAppDataRoaming360se6Application360se.exe‘ ##360浏览器的地址 chrome_options = Options() chrome_options.binary_location = __browser_url driver = webdriver.Chrome(chrome_options=chrome_options) driver.get(‘http://www.baidu.com‘) driver.find_element_by_id("kw").send_keys("seleniumhq" + Keys.RETURN) time.sleep(3) driver.quit()
上面是直接使用,如果你觉得在测试框架中这么用不方便动态使用的话,可以做一层封装;
一、各浏览器webdriver
说明:
1. selenium3.x中,firefox需要下载webdriver(geckodriver.exe)。
2. 360极速和360安全浏览器的内核是chrome,目前支持内核版本45.x。拷贝两份chromedriver 2.20,可以重命名为360chromedriver & 360sechromedriver。
PS:注意,360浏览器会自动升级。请关闭自动升级。
目录:C:UsersusernameAppDataRoaming360se6Application8.1.1.250installer,找到chromeup.dll文件,删除或者重命名。
二、增加代码支持让RIDE识别360浏览器
修改seleniumwebdriver目录下__init__.py代码。
目录:C:Python27Libsite-packagesseleniumwebdriver
from .firefox.webdriver import WebDriver as Firefox # noqa from .firefox.firefox_profile import FirefoxProfile # noqa from .chrome.webdriver import WebDriver as Chrome # noqa from .chrome.options import Options as ChromeOptions # noqa # 360 support from .chrome360.webdriver import WebDriver as Chrome360 # 360 extreme browser from .chrome360se.webdriver import WebDriver as Chrome360se # 360 security browser # from .ie.webdriver import WebDriver as Ie # noqa from .edge.webdriver import WebDriver as Edge # noqa from .opera.webdriver import WebDriver as Opera # noqa from .safari.webdriver import WebDriver as Safari # noqa from .blackberry.webdriver import WebDriver as BlackBerry # noqa from .phantomjs.webdriver import WebDriver as PhantomJS # noqa from .android.webdriver import WebDriver as Android # noqa from .remote.webdriver import WebDriver as Remote # noqa from .common.desired_capabilities import DesiredCapabilities # noqa from .common.action_chains import ActionChains # noqa from .common.touch_actions import TouchActions # noqa from .common.proxy import Proxy # noqa __version__ = ‘3.0.2‘
修改Selenium2Librarykeywords下browsermanagement.py代码
目录:C:Python27Libsite-packages
obotframework_selenium2library-1.8.1-py2.7.eggSelenium2Librarykeywords
代码段一:
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) FIREFOX_PROFILE_DIR = os.path.join(ROOT_DIR, ‘resources‘, ‘firefoxprofile‘) BROWSER_NAMES = {‘ff‘: "_make_ff", ‘firefox‘: "_make_ff", ‘ie‘: "_make_ie", ‘internetexplorer‘: "_make_ie", ‘googlechrome‘: "_make_chrome", ‘gc‘: "_make_chrome", ‘chrome‘: "_make_chrome", #360 Extreme Browser ‘chrome360‘: "_make_chrome360", #360 Security Browser ‘chrome360se‘: "_make_chrome360se", ‘opera‘ : "_make_opera", ‘phantomjs‘ : "_make_phantomjs", ‘htmlunit‘ : "_make_htmlunit", ‘htmlunitwithjs‘ : "_make_htmlunitwithjs", ‘android‘: "_make_android", ‘iphone‘: "_make_iphone", ‘safari‘: "_make_safari", ‘edge‘: "_make_edge" }
代码二:搜索def _make_chrome,增加两段代码
def _make_chrome(self , remote , desired_capabilities , profile_dir): return self._generic_make_browser(webdriver.Chrome, webdriver.DesiredCapabilities.CHROME, remote, desired_capabilities) #360 Extreme Browser def _make_chrome360(self , remote , desired_capabilities , profile_dir): return self._generic_make_browser(webdriver.Chrome360, webdriver.DesiredCapabilities.CHROME, remote, desired_capabilities) #360 Security Browser def _make_chrome360se(self , remote , desired_capabilities , profile_dir): return self._generic_make_browser(webdriver.Chrome360se, webdriver.DesiredCapabilities.CHROME, remote, desired_capabilities)
三、360浏览器的实现代码
目录:C:Python27Libsite-packagesseleniumwebdriver
文件1:__init__.py,可以为空,也可以增加说明文字。例如,作者,版本,功能等。
文件2:新建webdriver.py,实现360极速浏览器。代码如下:from selenium.webdriver import Chrome as ChromeWebdriver
from selenium.webdriver.chrome.options import Options import os class WebDriver(ChromeWebdriver): def __init__(self, b360bin=None, executable_path="360chromedriver", port=0, chrome_options=None, service_args=None, desired_capabilities=None, service_log_path=None): # 360 broswer direction if b360bin: self.bin = b360bin else: self.bin = r‘%s360ChromeChromeApplication360chrome.exe‘ % os.getenv(‘LOCALAPPDATA‘) chrome_options = Options() chrome_options.binary_location = self.bin ChromeWebdriver.__init__(self, executable_path, port, chrome_options, service_args, desired_capabilities, service_log_path)
360安全浏览器类似:
from selenium.webdriver import Chrome as ChromeWebdriver from selenium.webdriver.chrome.options import Options import os class WebDriver(ChromeWebdriver): def __init__(self, b360bin=None, executable_path="360sechromedriver", port=0, chrome_options=None, service_args=None, desired_capabilities=None, service_log_path=None): # 360se broswer direction if b360bin: self.bin = b360bin else: self.bin = r‘C:UsersAdministratorAppDataRoaming360se6Application360se.exe‘ chrome_options = Options() chrome_options.binary_location = self.bin ChromeWebdriver.__init__(self, executable_path, port, chrome_options, service_args, desired_capabilities, service_log_path)
转载:https://blog.csdn.net/Allan_shore_ma/article/details/63757206
以上是关于selenium兼容非标准chrome内核的浏览器的主要内容,如果未能解决你的问题,请参考以下文章
python selenium中如何测试360等基于chrome内核的浏览器