【问题标题】:python selenium firefox - add_extension not workingpython selenium firefox - add_extension 不工作
【发布时间】:2018-12-19 04:59:03
【问题描述】:

尝试将 uBlock 添加到浏览器会话,但它不起作用。

import selenium
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.options import Options as options


def establish_browser(type, hide):
    browser = ''
    if type == 'firefox':
        ops = options()
        ops.add_argument("--headless") if hide is True else ops.add_argument("--head")
        profile = selenium.webdriver.FirefoxProfile()
        profile.add_extension(extension='uBlock0@raymondhill.net.xpi')
        browser = selenium.webdriver.Firefox(firefox_profile=profile, executable_path='geckodriver.exe', options=ops, firefox_binary=FirefoxBinary('C:/Program Files/Mozilla Firefox/firefox.exe'))
    return browser

browser = establish_browser('firefox', False)

应如何更改以使 uBlock 正常工作?

更新

chrome 版本似乎正在运行……

if type == 'chrome':
    from selenium.webdriver.chrome.options import Options as options
    ops = options()
    ops.add_argument("--headless") if hide is True else ops.add_argument("--head")
    ops.add_extension("ublock.crx")
    browser = selenium.webdriver.Chrome(executable_path='chromedriver.exe', options=ops, desired_capabilities={'binary_location': 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'})

Firefox 折旧了吗?

【问题讨论】:

  • 您是否在 geckodriver 问题跟踪器上创建了一个关于此的问题?
  • 我可能会这样做,我明天会研究它

标签: python selenium selenium-webdriver selenium-firefoxdriver


【解决方案1】:

由于某种原因,chrome 的 add_extension 有效,但 firefox 的 add_extension 无效(目前)……这是我为 firefox 添加扩展的解决方法。

  1. 通过right click windows start button > run > firefox.exe -P创建一个新的firefox配置文件
  2. 然后添加您想要的任何扩展程序,ublock、adblock plus 等
  3. 调用您的个人资料文件夹

profile = selenium.webdriver.FirefoxProfile("C:/test")

browser = selenium.webdriver.Firefox(firefox_profile=profile, options=ops)

显然profile.add_extension() 不是此解决方法的必备条件

更新! - 添加了 chrome 配置文件

出于对称目的,我更新了 chrome 示例代码以使用 chrome 配置文件,而不是直接调用 .crx

  1. 在 chrome 的默认配置文件中安装扩展程序。
  2. 导航到 C:\Users\User\AppData\Local\Google\Chrome 或任何 chromes User Data 文件夹所在的位置。直接调用这个文件夹(绝对路径)或者重命名,调用相对路径。我已将其重命名为chrome_profile

    ops = options()
    ops.add_argument("--headless") if hide is True else ops.add_argument("--head")
    ops.add_argument('user-data-dir=chrome_profile')
    ops.add_argument('--profile-directory=Default')
    ops.add_argument("--incognito")
    browser = selenium.webdriver.Chrome(executable_path='chromedriver.exe', options=ops, desired_capabilities={'binary_location': 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'})
    

【讨论】:

    【解决方案2】:

    要添加到@Rhys 的解决方案,更简单的方法可能是official documentation 中的以下选项,它可以按预期工作:

    driver = webdriver.Firefox('path/to/executable')
    driver.install_addon('~/path/to/addon.xpi')
    

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • 同意,这就是为什么下面包含必要的代码。
    猜你喜欢
    • 2020-02-09
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多