天天看點

python selenium中如何測試360浏覽器等第三方非正式的浏覽器

注意360se是基于chrome核心的浏覽器,基于ie的請替換成iedriver!

1. from selenium.webdriver.chrome.options import Options
2. from selenium import webdriver
3. from selenium.webdriver.common.keys import Keys
4. import time
5. 
6. __browser_url = r'C:\Users\Administrator\AppData\Roaming\360se6\Application\360se.exe'  ##360浏覽器的位址
7. chrome_options = Options()
8. chrome_options.binary_location = __browser_url
9. 
10. driver = webdriver.Chrome(chrome_options=chrome_options)
11. driver.get('http://www.baidu.com')
12. driver.find_element_by_id("kw").send_keys("seleniumhq" + Keys.RETURN)
13. time.sleep(3)
14. driver.quit()
上面是直接使用,      

如果你覺得在測試架構中這麼用不友善動态使用的話,可以做一層封裝;

python selenium中如何測試360浏覽器等第三方非正式的浏覽器

1、C:\Python27\Lib\site-packages\selenium\webdriver這個目錄中的__init__.py檔案添加一行

from .chrome360.webdriver import WebDriver as Chrome360

2、同樣在該目錄下添加一個目錄:chrome360,其下建立2個檔案,__init__.py檔案可以為空,webdriver.py檔案内容如下:

1. from selenium.webdriver import Chrome as ChromeWebdriver
2. from selenium.webdriver.chrome.options import Options
3. import os
4. 
5. class WebDriver(ChromeWebdriver):
6. 
7. def __init__(self, b360bin=None, executable_path="chromedriver", port=0,
8. None, service_args=None,
9. None, service_log_path=None):
10. if b360bin:
11. self.bin = b360bin
12. else:
13. self.bin = r'%s\360Chrome\Chrome\Application\360chrome.exe' % os.getenv('LOCALAPPDATA')  ##      

你也可以讀系統資料庫來擷取360的安裝位置

  1. chrome_options = Options()
  2. self.bin
  3. self, executable_path, port,
  4. chrome_options, service_args,
  5. desired_capabilities, service_log_path)

這樣我們就可以在webdriver對象中直接調用,方法如下:

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

這樣就跟調用其它浏覽器的代碼一樣簡介

PS:同樣你還可以做一個py的安裝更新檔包,這樣在搭建環境的時候,同時安裝上這個更新檔包就直接可以使用了。