python - selenium - 无法将 browsec 扩展添加到 firefox 配置文件

标签 python selenium selenium-webdriver

我想使用 selenium 来抓取一些网站。我无法通过自己的互联网连接访问该网站,因此我需要为此使用 browsec mozilla 插件。

我无法在启用附加组件的情况下使用 selenium 启动 firefox。 这是我尝试过的:

import selenium
from selenium import webdriver

url = "http://url"
profile = webdriver.FirefoxProfile()
profile.add_extension('browsec@browsec.com.xpi')
#profile.add_extension("C:\Users\urs\AppData\Roaming\Mozilla\Firefox\Profiles\abc.default\extensions\browsec@browsec.com.xpi")
driver = webdriver.Firefox(firefox_profile=profile)

if __name__ == "__main__":
   driver.get(url)
   driver.wait(5)
   driver.quit()

我已经尝试将扩展放在我的脚本所在的同一目录中并使用以下内容

profile.add_extension('browsec@browsec.com.xpi')

当我运行时出现这个错误:

Traceback (most recent call last): File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile .py", line 346, in _addon_details with open(os.path.join(addon_path, 'install.rdf'), 'r') as f: FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\Usr\AppD ata\Local\Temp\tmp0hny31u3.browsec@browsec.com.xpi\install.rdf'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "test.py", line 7, in profile.add_extension("browsec@browsec.com.xpi") File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile .py", line 95, in add_extension self._install_extension(extension) File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile .py", line 274, in _install_extension addon_details = self._addon_details(addon) File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile .py", line 351, in _addon_details raise AddonFormatError(str(e), sys.exc_info()[2]) selenium.webdriver.firefox.firefox_profile.AddonFormatError: ("[Errno 2] No such file or directory: 'C:\\Users\\Usr\\AppData\\Local\\Temp\\tmp0hn y31u3.browsec@browsec.com.xpi\\install.rdf'", )

我也试过给出扩展的路径:

profile.add_extension("C:\Users\urs\AppData\Roaming\Mozilla\Firefox\Profiles\abc.default\extensions\browsec@browsec.com.xpi")

我遇到了这个错误:

profile.add_extension("C:\Users\Hassan\AppData\Roaming\Mozilla\Firefox\Profi les\n5jwlj9l.default\extensions\browsec@browsec.com.xpi") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in positio n 2-3: truncated \UXXXXXXXX escape

像下面这样格式化路径字符串也无济于事。

profile.add_extension(r"C:\Users\urs\AppData\Roaming\Mozilla\Firefox\Profiles\abc.default\extensions\browsec@browsec.com.xpi")

我得到以下信息:

Traceback (most recent call last): File "test.py", line 7, in profile.add_extension(r"C:\Users\Hassan\AppData\Roaming\Mozilla\Firefox\Prof iles\n5jwlj9l.default\extensions\browsec@browsec.com.xpi") File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile .py", line 95, in add_extension self._install_extension(extension) File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile .py", line 274, in _install_extension addon_details = self._addon_details(addon) File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\firefox_profile .py", line 351, in _addon_details raise AddonFormatError(str(e), sys.exc_info()[2]) selenium.webdriver.firefox.firefox_profile.AddonFormatError: ("[Errno 2] No such file or directory: 'C:\\Users\\usr\\AppData\\Local\\Temp\\tmp1he 0fym_.browsec@browsec.com.xpi\\install.rdf'", )

如何配置 selenium 以默认启用 browsec 运行 firefox?

最佳答案

我找到了 this文章很有帮助。

不是将扩展添加到配置文件,而是在创建浏览器后安装它:

from selenium import webdriver


driver = webdriver.Firefox()
# This installs adblock plus
driver.install_addon("/home/your_username/coding/Project/seleniumTest/adblock.xpi", temporary=True)
driver.get('https://www.stackoverflow.com')

请务必将 .xpi 添加到您的项目文件夹中!

关于python - selenium - 无法将 browsec 扩展添加到 firefox 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49467975/

相关文章:

selenium-webdriver - Selenium Web 驱动程序--TestNG 报告中未捕获失败屏幕截图

python - 从 Python 运行 Xvfb

java - 从示例中找不到 Selenium 类名

python - 导入错误 : No module named 'selenium' in PyCharm

javascript - 如何在 Protractor 中的 ng-repeat 中使用 isElementPresent/isPresent?

twitter-bootstrap - Capybara:不会选择 Bootstrap 按钮组

javascript - 是否可以使用 webdriver.js 观察 postMessages?

python - Python,pygame.mixer:声音不起作用

python - 如何pickle NotImplementedType

python - 高效的上下文无关语法解析器,最好是 Python 友好的