Selenium :: Python :: Chrome 扩展

马斯特

我试图加载google chrome extensionSelenium WebDriver

但我收到一个错误OSError: Path to the extension doesn't exist

这是我正在使用的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from os import path

path = "path to chrome driver"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_extension('Adblock-Plus_v1.12.4_0.crx') # ALTERNATIVE 0
driver = webdriver.Chrome(path, chrome_options=chrome_options)

在阅读了本网站上的各种类似问题后,我尝试了以下两种选择:

# Alternative 1
chrome_options.add_extension('~/Library/Application\ Support/Google/Chrome/Default/Extensions/[Extension ID]/Adblock-Plus_v1.12.4_0.crx')

#Alternative 2
chrome_options.add_extension(path.abspath("Adblock-Plus_v1.12.4_0.crx"))

但是它们都不起作用。备选方案 1 给出与原始代码相同的错误消息,而备选方案 2 给出错误AttributeError: 'str' object has no attribute 'abspath'

有没有人知道我可以做些什么不同的事情?

弯叶

可能更感谢,这是因为 python 引用了错误的路径,通常是路径中的主目录快捷方式~/Python 将尝试从当前目录运行该文件,例如,如果您的代码在 中~/Dev/testproject,并且上面调用的代码实际上是在尝试运行/home/username/Dev/testproject/~/Library/Application Support/Google/Chrome/Default/Extensions/[Extension ID]/Adblock-Plus_v1.12.4_0.crx

尝试使用以下方法:

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

chromedriver = "path to chrome driver"
chrome_options = webdriver.ChromeOptions()


# choose one of the following 2:
chrome_options.add_extension(os.path.expanduser('~/Library/Application\ Support/Google/Chrome/Default/Extensions/[Extension ID]/Adblock-Plus_v1.12.4_0.crx'))  # Option 1: if your extension is not also in your project folder
chrome_options.add_extension(os.path.abspath('Adblock-Plus_v1.12.4_0.crx'))  # Option 2: if your extension IS in your project folder


driver = webdriver.Chrome(chromedriver, chrome_options=chrome_options)

编辑:避免声明一个命名的变量,path因为你是pathos. 这就是您在替代方案 #2 上收到错误的原因。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python Selenium Chrome Web驱动程序

使用Selenium,Chrome和Python下载PDF

python linux selenium:无法访问chrome

使用Selenium时如何选择要启用的Chrome扩展程序

在Python + Selenium Chrome WebDriver中处理通知

为Selenium创建HTTP基本身份验证Chrome扩展(提供MWE)

Selenium:chrome和PhantomJS之间的区别?-python

Chrome Webdriver Selenium内存错误-Python 3

使用Selenium和python在Chromedriver中保存扩展设置

使用Selenium WebDriver访问和处理Chrome扩展程序弹出窗口

WebDriverException:消息:未知错误:使用Selenium ChromeDriver和Chrome通过Python无法从未知错误获取自动化扩展

使用python中的Selenium Webdriver下载在embed标签中具有stream-url是chrome扩展名的文件

如何使用Selenium和Python安装Chrome扩展程序

消息从本地python传递到chrome扩展

如何在python中进行chrome扩展?

如何使用Selenium或其他工具来操作Chrome扩展程序的小部件?

Python Selenium无法连接到Webdriver Firefox扩展

无法使用Selenium Webdriver将解压后的扩展程序加载到chrome中

使用def选项打开Selenium Python Chrome

Python - 如何在打开 Chrome 窗口后使用 Selenium 禁用扩展

python selenium chrome警报不可调用?

Selenium:Chrome 扩展的 NoSuchFrameException

Chrome 浏览器中的 Selenium 插件/扩展

如何向 selenium chrome webdriver 添加多个扩展

如何使用 selenium VBA 打开带有扩展名的 chrome

通过python selenium在chrome中节流CPU

有没有办法点击我刚刚使用 java/selenium 安装的 chrome 扩展?

动态 ID Selenium Python Chrome

使用 Selenium Python 禁用 Chrome 通知