selenium中如何測試360等基於chrome內核的瀏覽器

直接上代碼,注意是基於chrome內核的瀏覽器,基於ie的請替換其中的chrome方法爲ie,但本身何嘗試過,若是有結果能夠告知!python

from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
 
__browser_url = r'C:\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()

上面是直接使用,若是你以爲在測試框架中這麼用不方便動態使用的話,能夠作一層封裝;
一、C:\Python27\Lib\site-packages\selenium-2.48.0-py2.7.egg\selenium\webdriver這個目錄中的__init__.py文件添加一行web

from .chrome360.webdriver import WebDriver as Chrome360chrome

在這裏向你們推薦一個資料分享羣:175317069.瀏覽器

二、一樣在該目錄下添加一個目錄:chrome360,其下新建2個文件,__init__.py文件能夠爲空,webdriver.py文件內容以下:框架

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="chromedriver", port=0,
                 chrome_options=None, service_args=None,
                 desired_capabilities=None, service_log_path=None):
        if b360bin:
            self.bin = b360bin
        else:
            self.bin = r'%s\360Chrome\Chrome\Application\360chrome.exe' % os.getenv('LOCALAPPDATA')  ##你也能夠讀註冊表來獲取360的安裝位置
        chrome_options = Options()
        chrome_options.binary_location = self.bin
        ChromeWebdriver.__init__(self, executable_path, port,
                    chrome_options, service_args,
                    desired_capabilities, service_log_path)


這樣咱們就能夠在webdriver對象中直接調用,方法以下:測試

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
    
driver = webdriver.Chrome360()
driver.get('http://www.baidu.com')
driver.find_element_by_id("kw").send_keys("seleniumhq" + Keys.RETURN)
time.sleep(3)
driver.quit()


這樣就跟調用其它瀏覽器的代碼同樣簡介
PS:一樣你還能夠作一個py的安裝補丁包,這樣在搭建環境的時候,同時安裝上這個補丁包就直接能夠使用了。ui

重要的事情說三遍必需要安裝了chromedriver.exe文件,必需要安裝了chromedriver.exe文件,必需要安裝了chromedriver.exe文件以及360瀏覽器
 url

相關文章
相關標籤/搜索