注意360se是基於chrome內核的瀏覽器,基於ie的請替換成iedriver!
- 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()
上面是直接使用,如果你覺得在測試框架中這么用不方便動態使用的話,可以做一層封裝;
1、C:\Python27\Lib\site-packages\selenium\webdriver這個目錄中的__init__.py文件添加一行
from .chrome360.webdriver import WebDriver as Chrome360
2、同樣在該目錄下添加一個目錄: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的安裝補丁包,這樣在搭建環境的時候,同時安裝上這個補丁包就直接可以使用了。
必須要安裝了chromedriver.exe文件,必須要安裝了chromedriver.exe文件,必須要安裝了chromedriver.exe文件以及360瀏覽器