【问题标题】:Loading extensions in firefox via selenium and python通过 selenium 和 python 在 Firefox 中加载扩展
【发布时间】:2019-03-07 14:49:51
【问题描述】:

我正在尝试在 python 中使用 selenium 浏览器将 firefox 插件加载到无头 firefox 中。

from selenium import webdriver
import time
geckodriver = 'C:\\Gecko\\geckodriver.exe'

options = webdriver.FirefoxOptions()
options.add_argument('-headless')


profile = webdriver.FirefoxProfile('C:/AppData/Roaming/Mozilla/Firefox/Profiles/am97bfb5.default-1538379314459')
profile.set_preference("network.http.use-cache", False)

profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.socks", "127.0.0.1")
profile.set_preference("network.proxy.socks_port", 9150)
profile.set_preference("network.proxy.socks_version", 5)
profile.set_preference("javascript.enabled", True);
profile.set_preference("network.proxy.socks_remote_dns", False)
profile.set_preference("general.useragent.override","Mozilla/5.0 (Linux; Android 6.0; HTC One M9 Build/MRA58K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.98 Mobile Safari/537.36")
profile.exp="D:/test/XPI/uBlock0@raymondhill.net.xpi"
profile.add_extension(extension=profile.exp)
time.sleep(5)


browser = webdriver.Firefox(firefox_profile=profile,executable_path=geckodriver, firefox_options=options)
print (profile.path)
browser.get('https://intoli.com/blog/making-chrome-headless-undetectable/chrome-headless-test.html')

browser.save_screenshot('D:\\Test\\headless_firefox_test.png')

browser.quit()

但是运行代码后出现以下错误:

文件“C:\Anaconda\lib\site-packages\selenium\webdriver\firefox\firefox_profile.py”,第 351 行,在 _addon_details 引发 AddonFormatError(str(e), sys.exc_info()[2])

AddonFormatError: ("[Errno 2] 没有这样的文件或目录:'C:\\Users\\AppData\\Local\\Temp\\tmp6cn69jiy.uBlock0@raymondhill.net.xpi\\install.rdf'" , )

你有什么想法吗?我一直在查看网站上的其他帖子,但没有找到任何适用于该代码的内容。

谢谢!

【问题讨论】:

  • 您使用的是最新版本吗?据我记得这是Firefox驱动程序中的一个错误,他们前段时间修复了它
  • 我使用的是 Firefox Quantum 62.0.2(64 位)
  • 我刚刚安装了 Firefox 的最新更新,它现在可以运行了。谢谢基里尔!

标签: python selenium headless selenium-firefoxdriver


【解决方案1】:

尝试使用 Firefox 开发者版而不是标准的 Firefox。它在 C# 中对我有用。希望此解决方案对您有所帮助。 此外,正如我在您的代码中看到的那样,您使用特定配置文件午餐 Firefox,因此最简单的方法是将您的扩展手动添加到您的配置文件中,然后您不必以编程方式进行

【讨论】:

    【解决方案2】:

    它应该可以在较新的 Firefox 版本中使用。更新您的 Firefox,然后重试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2018-07-30
      • 2018-10-25
      • 2021-11-27
      • 2011-09-11
      • 1970-01-01
      • 2021-11-02
      相关资源
      最近更新 更多