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浏览器

posted @ 2016-05-11 17:39  白灰  阅读(6254)  评论(1编辑  收藏  举报