Python - Remote Webdriver with Extension installed in it(Python - 安装了扩展的远程 Webdriver)
问题描述
我想使用 BrowserStack 在不同的浏览器版本上测试一个扩展.这是一个返回具有指定功能的驱动程序的函数.我在本地机器上有一个用于 Chrome 的 .crx 文件 和一个用于 Firefox 的 .xpi 文件.我想使用 Remote Webdriver 并使用 Python 安装相应的扩展.
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
推荐答案
对于 Firefox,您需要创建一个配置文件并使用 add_extension.然后将配置文件传递给 WebDriver 构造函数:
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)
或者,您可以提前创建一个 Firefox 配置文件,然后手动将您的扩展添加到其中.稍后您将其路径作为参数传递给 FirefoxProfile()
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')
对于 Chrome,请使用 ChromeOptions:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python - 安装了扩展的远程 Webdriver
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
