admin管理员组文章数量:1026989
python selenium中如何测试360等基于chrome内核的浏览器
直接上代码,注意是基于chrome内核的浏览器,基于ie的请替换其中的chrome方法为ie,但自己未尝试过,如果有结果可以告知!
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdrivermon.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_urldriver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('')
driver.find_element_by_id("kw").send_keys("seleniumhq" + Keys.RETURN)
time.sleep(3)
driver.quit()
上面是直接使用,如果你觉得在测试框架中这么用不方便动态使用的话,可以做一层封装;
1、C:\Python27\Lib\site-packages\selenium-2.48.0-py2.7.egg\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 osclass 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 = b360binelse:self.bin = r'%s\360Chrome\Chrome\Application\360chrome.exe' % os.getenv('LOCALAPPDATA') ##你也可以读注册表来获取360的安装位置chrome_options = Options()chrome_options.binary_location = self.binChromeWebdriver.__init__(self, executable_path, port,chrome_options, service_args,desired_capabilities, service_log_path)
这样我们就可以在webdriver对象中直接调用,方法如下:
from selenium import webdriver
from selenium.webdrivermon.keys import Keys
import timedriver = webdriver.Chrome360()
driver.get('')
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浏览器
python selenium中如何测试360等基于chrome内核的浏览器
直接上代码,注意是基于chrome内核的浏览器,基于ie的请替换其中的chrome方法为ie,但自己未尝试过,如果有结果可以告知!
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdrivermon.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_urldriver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('')
driver.find_element_by_id("kw").send_keys("seleniumhq" + Keys.RETURN)
time.sleep(3)
driver.quit()
上面是直接使用,如果你觉得在测试框架中这么用不方便动态使用的话,可以做一层封装;
1、C:\Python27\Lib\site-packages\selenium-2.48.0-py2.7.egg\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 osclass 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 = b360binelse:self.bin = r'%s\360Chrome\Chrome\Application\360chrome.exe' % os.getenv('LOCALAPPDATA') ##你也可以读注册表来获取360的安装位置chrome_options = Options()chrome_options.binary_location = self.binChromeWebdriver.__init__(self, executable_path, port,chrome_options, service_args,desired_capabilities, service_log_path)
这样我们就可以在webdriver对象中直接调用,方法如下:
from selenium import webdriver
from selenium.webdrivermon.keys import Keys
import timedriver = webdriver.Chrome360()
driver.get('')
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浏览器
本文标签: python selenium中如何测试360等基于chrome内核的浏览器
版权声明:本文标题:python selenium中如何测试360等基于chrome内核的浏览器 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/IT/1694653033a254560.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论