Python的 - 与扩展安装在它远程的webdriver [英] Python - Remote Webdriver with Extension installed in it

查看:472
本文介绍了Python的 - 与扩展安装在它远程的webdriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Browserstack 来测试不同的浏览器版本一个分机。
这是与特定功能返回的驱动程序的功能。
我有一个 .crx档案为Chrome和的.xpi文件为Firefox我的本地机器上。
我想用远程的webdriver 安装了相应的扩展,使用Python。

 高清my_webdriver(浏览器,BROWSER_VERSION,操作系统,OS_VERSION):
    帽= {}
    帽[浏览] =浏览器
    帽[BROWSER_VERSION] = BROWSER_VERSION
    帽[OS] = OS
    帽[OS_VERSION] = OS_VERSION
    帽[browserstack.debug] =真
    司机= webdriver.Remote(
    | command_executor ='blahblahblah',
    | desired_capabilities =上限)
    driver.maximize_window()
    返回驱动程序


解决方案

对于Firefox,你需要创建一个配置文件,并使用您的扩展添加到它<一个href=\"http://selenium.google$c$c.com/git/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.firefox_profile.html#selenium.webdriver.firefox.firefox_profile.FirefoxProfile.add_extension\"相对=nofollow> add_extension 。然后,您通过配置文件的构造函数的webdriver:

 从selenium.webdriver.remote.webdriver进口的webdriver为RemoteWebDriver
selenium.webdriver.firefox.firefox_profile进口FirefoxProfile
...
FP = FirefoxProfile()
fp.add_extension('/路径/要/你/ extension.xpi')
司机= RemoteWebDriver(...,browser_profile = FP)

另外,你可以创建提前Firefox配置文件,并手动将extenstion添加到它。后来你传递给 FirefoxProfile其作为参数路径()

  FP = FirefoxProfile('/路径/要/你/ profile文件')

有关浏览器,使用 ChromeOptions

 从selenium.webdriver.chrome.options导入选项为ChromeOptions
chrome_options = ChromeOptions()
chrome_options.add_extension('/路径/要/你/ extension.crx')
司机= RemoteWebDriver(...,desired_capabilities =帽+ chrome_options.to_capabilities())

I want to test one extension on different browser versions using Browserstack. This is a function that returns driver with specified capabilities. I have a .crx file for Chrome and an .xpi file for Firefox on my local machine. I want to use Remote Webdriver with a corresponding extension installed, using Python.

def my_webdriver(browser, browser_version, os, os_version):
    caps = {}
    caps["browser"] = browser
    caps["browser_version"] = browser_version
    caps["os"] = os
    caps["os_version"] = os_version
    caps["browserstack.debug"] = "true"
    driver = webdriver.Remote(
    ¦   command_executor = 'blahblahblah',
    ¦   desired_capabilities = caps)
    driver.maximize_window()
    return driver

解决方案

For Firefox, you need to create a profile and add your extension to it using add_extension. Then you pass the profile to the WebDriver constructor:

from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
selenium.webdriver.firefox.firefox_profile import FirefoxProfile
...
fp = FirefoxProfile()
fp.add_extension('/path/to/your/extension.xpi')
driver = RemoteWebDriver(..., browser_profile=fp)

Alternatively, you can create a Firefox profile in advance, and manually add your extenstion to it. Later you pass its path as parameter to FirefoxProfile()

fp = FirefoxProfile('/path/to/your/profile')

For Chrome, use ChromeOptions:

from selenium.webdriver.chrome.options import Options as ChromeOptions
chrome_options = ChromeOptions()
chrome_options.add_extension('/path/to/your/extension.crx')
driver = RemoteWebDriver(..., desired_capabilities = caps + chrome_options.to_capabilities())

这篇关于Python的 - 与扩展安装在它远程的webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆