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

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

问题描述

我访问 Chrome扩展程序下载程序,为Adblock-Plus_v1.4.1拦截.crx文件。



我把它扔进工作的目录中,然后运行:

  from selenium import webdriver 

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

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



ERROR:extension_error_reporter.cc(56)]扩展错误:包无效:'CRX_PUBLIC_KEY_INVALID'。

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

selenium.common.exceptions.WebDriverException:Message :u'Extension不能
安装'



我几乎100%确定我的代码没有问题,因为fa ct在抛出异常之前首先放置一个ChromeDriver类型的消息。



我也尝试通过转到'C:\Documents和设置\\用户名* \\\\本地设置\\应用程序数据\\Google \ Chrome\\用户数据\\\\默认\\扩展\\ // extensions / 与开发人员模式,尝试使用该.crx创建并得到完全相同的错误消息



我也试过不同的方式:

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

这不会导致一个异常,甚至一个Chrome驱动程序错误,但如果我手动去 chrome:// extensions / 它不会说扩展被加载... p>

我认为我的问题与实际的.crx文件本身有关。因为错误消息的性质...但在同一时间,我不知道,因为如果我产生一个 webdriver.Chrome()会话,然后手动去至 chrome:// extensions / 我可以物理地拖放安装相同的.crx文件。


$ b 编辑:我意识到我实际上并没有问一个问题,所以这里是:



我做错了什么?为什么不能加载这个Chrome扩展?它是我的代码,还是.crx文件本身?



更新:@Pat Meeker
我试过了,但是在从java到python


capability = webdriver.DesiredCapabilities.CHROME 返回一个包含我所有参数的字典,确定我需要做的仅仅是添加参数。

  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打开了,它似乎记住了它以前的位置,但没有更多,没有书签,没有扩展没有任何东西。

在你的程序中添加这行额外的行



from selenium.webdriver.chrome.options import选项它将工作...



像这样

  from selenium import webdriver 
from selenium.webdriver.chrome.options import选项

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


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

I threw it in the directory im 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)

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'.

Then eventually a webdriver exception:

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

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.

I also tried to pack it myself by going to 'C:\Documents and Settings\\*UserName*\Local Settings\Application Data\Google\Chrome\User Data\Default\Extensions' 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)

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

Im 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, im 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.

Edit: I realized I didnt actually ask a question so here it is:

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

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

capability = webdriver.DesiredCapabilities.CHROME returns a dictionary that has all my arguments in i, so im 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/')

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 it will work...

like this

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(.crx文件)运行Selenium WebDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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