简体   繁体   English

Firefox Webdriver 安装插件到远程 Webdriver

[英]Firefox Webdriver Install Addon to Remote Webdriver

I have the following code that connects to a Remote Webdriver and installs an extension我有以下连接到远程 Webdriver 并安装扩展的代码

options = webdriver.FirefoxOptions()
options.set_preference('intl.accept_languages', 'en,en-US')
options.add_argument('--log-level=3')  # Not logs will be displayed.
options.add_argument('--mute-audio')  # Audio is muted.
options.add_argument('--enable-webgl-draft-extensions')
options.add_argument('--disable-infobars')  # Disable popup
options.add_argument('--disable-popup-blocking')  # and info bars.

profile = webdriver.FirefoxProfile()
profile.add_extension('/path/to/tampermonkey.xpi')

driver = webdriver.Remote("http://127.0.0.1:4445/wd/hub", options=options, browser_profile=profile)

But when I go into the browser, the extension was never installed.但是当我进入浏览器时,从未安装过扩展程序。 Am I misunderstanding how to install extension in geckodriver?我误解了如何在 geckodriver 中安装扩展吗?

For Firefox, you should not use add_extension , as mentioned in this issue :对于 Firefox,您不应使用add_extension ,如本期所述:

the currently supported approach now is to add the extension from the install_addon() method on the firefox driver after the session has been created.当前支持的方法是在创建会话后在 firefox 驱动程序上添加install_addon()方法的扩展。

However, install_addon is only available for local webdrivers.但是, install_addon仅适用于本地网络驱动程序。 A simple workaround is required when using remote webdrivers, as mentioned in this issue .本期所述,使用远程网络驱动程序时需要一个简单的解决方法。 The trick requires you to change:这个技巧需要你改变:

profile = webdriver.FirefoxProfile()
profile.add_extension('/path/to/tampermonkey.xpi')

driver = webdriver.Remote("http://127.0.0.1:4445/wd/hub", options=options, browser_profile=profile)

to

driver = webdriver.Remote("http://127.0.0.1:4445/wd/hub", options=options)
addon_id = webdriver.Firefox.install_addon(driver, "/path/to/tampermonkey.xpi")

The full code should be something like below:完整的代码应该如下所示:

from selenium import webdriver

options = webdriver.FirefoxOptions()
options.set_preference('intl.accept_languages', 'en,en-US')
options.add_argument('--log-level=3')  # Not logs will be displayed.
options.add_argument('--mute-audio')  # Audio is muted.
options.add_argument('--enable-webgl-draft-extensions')
options.add_argument('--disable-infobars')  # Disable popup
options.add_argument('--disable-popup-blocking')  # and info bars.

driver = webdriver.Remote("http://127.0.0.1:4445/wd/hub", options=options)
addon_id = webdriver.Firefox.install_addon(driver, "/path/to/tampermonkey.xpi")

# The add-on is installed

# and optionally uninstall the add-on by uncommenting the code below
# webdriver.Firefox.uninstall_addon(driver, addon_id)

I have opened a pull request to the Selenium Docs to clarify such usages.我已经向 Selenium Docs 打开了一个 pull request来澄清这些用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM