使用带有扩展名的 Python 运行 Selenium WebDriver(.crx 文件) [英] Running Selenium WebDriver using Python with extensions (.crx files)

查看:25
本文介绍了使用带有扩展名的 Python 运行 Selenium WebDriver(.crx 文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我前往 Chrome 扩展下载器 获取Adblock-Plus_v1.crx"文件.4.1'.

I went to Chrome Extension Downloader to snag the .crx file for 'Adblock-Plus_v1.4.1'.

我把它放在我正在工作的目录中,然后运行:

I threw it in the directory I'm working in, and then ran:

from selenium import webdriver

chop = webdriver.ChromeOptions()
chop.add_extension('Adblock-Plus_v1.4.1.crx')
driver = webdriver.Chrome(chrome_options = chop)

它完全承认它存在,但它给了我看起来像 ChromeDriver.exe 样式的消息:

It totally acknowledges that it exists, but it gives me what looks like a ChromeDriver.exe style message:

ERROR:extension_error_reporter.cc(56)] Extension error: Package is invalid: 'CRX_PUBLIC_KEY_INVALID'.

然后最终出现一个 webdriver 异常:

Then eventually a webdriver exception:

selenium.common.exceptions.WebDriverException: Message: u'Extension could not be installed'

我几乎 100% 确定我的代码没有任何问题,因为它在抛出异常之前首先放置了 ChromeDriver 类型的消息.

I am almost 100% sure that there is nothing wrong with my code, because of the fact it puts a ChromeDriver type message first before throwing the exception.

我也尝试自己打包,方法是转到 'C:Documents and Settings\*UserName*Local SettingsApplication DataGoogleChromeUser DataDefaultExtensions'chrome://extensions/ 上打开开发者模式,尝试使用创建的 .crx 并得到完全相同的错误消息

I also tried to pack it myself by going to 'C:Documents and Settings\*UserName*Local SettingsApplication DataGoogleChromeUser DataDefaultExtensions' on chrome://extensions/ with developer mode on, tried to use that .crx that was created and got the exact same error message

我也尝试了不同的方式:

I also tried a different way:

chop = webdriver.ChromeOptions()
chop.add_argument('--load_extension=Adblock-Plus_v1.4.1.crx')
driver = webdriver.Chrome(chrome_options = chop)

这不会导致异常,甚至不会导致 Chrome 驱动程序错误,但是如果我手动转到 chrome://extensions/,它不会说扩展程序已加载...

this doesn't cause an exception or even a Chrome Driver error, but if I manually go to chrome://extensions/ it doesn't say that the extension is loaded...

我认为我的问题与实际的 .crx 文件本身有关.由于错误消息的性质......但同时,我不确定因为如果我生成一个 webdriver.Chrome() 会话,然后手动转到 chrome://extensions/ 我可以通过物理方式拖放安装相同的 .crx 文件.

I'm thinking my problem has to do with the actual .crx file itself. because of the nature of the error message... but then at the same time, I'm not sure because if I spawn a webdriver.Chrome() session, and then manually go to chrome://extensions/ i can physically drag and drop install the same .crx file.

我意识到我实际上并没有提出问题,所以这里是:

I realized I didn't actually ask a question so here it is:

我做错了什么?为什么我无法加载这个 chrome 扩展?是我的代码,还是 .crx 文件本身?

What am I doing wrong? Why can't I load this chrome extension? Is it my code, or the .crx file itself?

更新:@Pat Meeker我试过这个,但我在从 java 到 python 的翻译中丢失了一些东西

UPDATE: @Pat Meeker I've tried this, but im losing something in the translation from java to python

capability = webdriver.DesiredCapabilities.CHROME 返回一个字典,在 i 中有我所有的参数,所以我很确定我唯一需要做的部分是添加参数.

capability = webdriver.DesiredCapabilities.CHROME returns a dictionary that has all my arguments in i, so I'm pretty sure the only part that I need to do is add the arguments.

options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=C:/Users/USER_NAME/AppData/Local/Google/Chrome/User Data/Default/')

这就是我现在拥有的,每当我尝试 driver = webdriver.Chrome(chrome_options=options) chrome 打开时,它似乎记得它以前的位置,但仅此而已,没有书签,没有扩展,什么都没有.

This is what I have right now, and whenever I try to driver = webdriver.Chrome(chrome_options=options) chrome opens up, and it seems to remember its previous position, but NOTHING more, no bookmarks, no extensions no nothing.

推荐答案

只需在您的程序中添加这一行

Just add this extra line in your program

from selenium.webdriver.chrome.options import Options 它将工作...

喜欢这个

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chop = webdriver.ChromeOptions()
chop.add_extension('Adblock-Plus_v1.4.1.crx')
driver = webdriver.Chrome(chrome_options = chop)

这篇关于使用带有扩展名的 Python 运行 Selenium WebDriver(.crx 文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆