码迷,mamicode.com
首页 > 其他好文 > 详细

selenium兼容非标准chrome内核的浏览器

时间:2018-06-28 13:58:01      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:新建   __init__   desire   360浏览器   root   bin   pretty   art   3.x   

多浏览器兼容性测试(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 = rC:\Users\Administrator\AppData\Roaming\360se6\Application\360se.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:\Users\username\AppData\Roaming\360se6\Application\8.1.1.250\installer,找到chromeup.dll文件,删除或者重命名。

技术分享图片

二、增加代码支持让RIDE识别360浏览器

修改selenium\webdriver目录下__init__.py代码。 
目录:C:\Python27\Lib\site-packages\selenium\webdriver

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

 

修改Selenium2Library\keywords下browsermanagement.py代码 
目录:C:\Python27\Lib\site-packages\robotframework_selenium2library-1.8.1-py2.7.egg\Selenium2Library\keywords

代码段一:

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:\Python27\Lib\site-packages\selenium\webdriver

技术分享图片

技术分享图片

文件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%s\360Chrome\Chrome\Application\360chrome.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 = rC:\Users\Administrator\AppData\Roaming\360se6\Application\360se.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内核的浏览器

标签:新建   __init__   desire   360浏览器   root   bin   pretty   art   3.x   

原文地址:https://www.cnblogs.com/hupilan521/p/9238018.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!