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


注意360se是基於chrome內核的瀏覽器,基於ie的請替換成iedriver!

 

[python]  view plain  copy
 
  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()  


上面是直接使用,如果你覺得在測試框架中這么用不方便動態使用的話,可以做一層封裝;

 

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

from .chrome360.webdriver import WebDriver as Chrome360

2、同樣在該目錄下添加一個目錄:chrome360,其下新建2個文件,__init__.py文件可以為空,webdriver.py文件內容如下:

 

[python]  view plain  copy
 
  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.                  chrome_options=None, service_args=None,  
  9.                  desired_capabilities=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的安裝位置  
  14.         chrome_options = Options()  
  15.         chrome_options.binary_location = self.bin  
  16.         ChromeWebdriver.__init__(self, executable_path, port,  
  17.                     chrome_options, service_args,  
  18.                     desired_capabilities, service_log_path)  



 

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

 

 

[python]  view plain  copy
 
  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的安裝補丁包,這樣在搭建環境的時候,同時安裝上這個補丁包就直接可以使用了。

必須要安裝了chromedriver.exe文件,必須要安裝了chromedriver.exe文件,必須要安裝了chromedriver.exe文件以及360瀏覽器


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM