与Selenium(Python)一起使用扩展
我目前正在使用Selenium运行Chrome实例来测试网页。每次我的脚本运行时,都会启动一个干净的Chrome实例(清除扩展、书签、浏览历史等)。我想知道是否可以用Chrome扩展运行我的脚本。我试着搜索一个Python示例,但在谷歌上搜索时什么都没有出现。您应该使用Chrome WebDriver设置要加载的扩展列表。下面是一个例子:与Selenium(Python)一起使用扩展,python,google-chrome,selenium,selenium-webdriver,Python,Google Chrome,Selenium,Selenium Webdriver,我目前正在使用Selenium运行Chrome实例来测试网页。每次我的脚本运行时,都会启动一个干净的Chrome实例(清除扩展、书签、浏览历史等)。我想知道是否可以用Chrome扩展运行我的脚本。我试着搜索一个Python示例,但在谷歌上搜索时什么都没有出现。您应该使用Chrome WebDriver设置要加载的扩展列表。下面是一个例子: import os from selenium import webdriver from selenium.webdriver.chrome.options
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
executable_path = "path_to_webdriver"
os.environ["webdriver.chrome.driver"] = executable_path
chrome_options = Options()
chrome_options.add_extension('path_to_extension')
driver = webdriver.Chrome(executable_path=executable_path, chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()
希望能有所帮助。主要答案对我不起作用,因为我没有意识到您必须将webdriver选项指向
.zip
文件
例如,chrome\u选项。添加扩展('path\u to\u extension\u dir')
不起作用。您需要:
chrome\u选项。添加扩展名('path\u to\u extension\u dir.zip')
在弄明白了这一点并阅读了关于如何通过命令行创建zip文件并将其加载到selenium
中的一篇文章之后,它对我有效的唯一方法就是在同一个python脚本中压缩扩展文件。事实证明,这是一种很好的方法,可以自动更新您对扩展所做的任何更改:
import os, zipfile
from selenium import webdriver
# Configure filepaths
chrome_exe = "path/to/chromedriver.exe"
ext_dir = 'extension'
ext_file = 'extension.zip'
# Create zipped extension
## Read in your extension files
file_names = os.listdir(ext_dir)
file_dict = {}
for fn in file_names:
with open(os.path.join(ext_dir, fn), 'r') as infile:
file_dict[fn] = infile.read()
## Save files to zipped archive
with zipfile.ZipFile(ext_file), 'w') as zf:
for fn, content in file_dict.iteritems():
zf.writestr(fn, content)
# Add extension
chrome_options = webdriver.ChromeOptions()
chrome_options.add_extension(ext_file)
# Start driver
driver = webdriver.Chrome(executable_path=chrome_exe, chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
driver.quit()
如果您想在selenium python脚本中导入任何chrome扩展
executable_path = "/webdrivers"
os.environ["webdriver.chrome.driver"] = executable_path
chrome_options = Options()
chrome_options.add_extension(' YOUR - EXTIONTION - NAME ')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("http://stackoverflow.com")
在使用selenium时,我还需要为chrome添加一个扩展。我所做的是首先使用selenium打开浏览器,然后像在google chrome中那样向浏览器添加扩展。这是因为selenium希望压缩扩展作为扩展参数,扩展参数将与
.crx
扩展一起使用
我的chrome浏览器中已经有了扩展。下面是我打包现有扩展所遵循的步骤
右键点击(3点)->“更多工具”->“扩展”上。
/home/user/.config/google chrome/Default/Extensions/fdjsidgdhskifcclfjowijfwidksdj/2.3.4_22.crx
extension='/home/user/.config/google-chrome/Default/Extensions/fdjsidgdhskifcclfjowijfwidksdj/2.3.4_22.crx'
options = webdriver.ChromeOptions()
options.add_extension(extension)
注意:您还可以在扩展url中找到'fdjsidgdhskifcclfjowijfwidksdj'id作为查询参数另一种方法是使用解包文件夹方法。我发现crx和zip方法在Selenium网格上根本不起作用。在这种方法中,您需要将扩展名从已手动安装的Chrome版本中的用户数据复制到Selenium Control Chrome的用户数据目录中,该扩展名由大量字母组成,如pehaalcefcjfccdpbckoablngfkfgfgj(您可以在运行时使用此代码的第一行选择它,它将自动填充)。它应该位于相同的等效目录(即扩展名)中。路径必须将您一直带到有manifest.json的目录,因此在本例中为“1.1.0”
chrome_options.add_argument("user-data-dir=C:/Users/charl/OneDrive/python/userprofile/profilename"
unpacked_extension_path = 'C:/Users/charl/OneDrive/python/userprofile/profilename/Default/Extensions/pehaalcefcjfccdpbckoablngfkfgfgj/1.1_0'
chrome_options.add_argument('--load-extension={}'.format(unpacked_extension_path))
driver = webdriver.Chrome(options=chrome_options)
这在无数个小时后对我有效。谢谢。不幸的是,它对我无效,你能提供一个需要进入ext_dir='extension'ext_file='extension.zip'
ext_dir
的值示例吗?应该是一个包含你想要加载的chrome扩展名的目录。我发布的脚本将该目录压缩并创建eext_file
,然后将其添加到浏览器选项中。最简单的扩展文件夹仅包含一个mainfest.json
和一个background.js
脚本。有关扩展的更多信息,通常可以查看本入门指南()和本示例().Hi,我测试了你的代码。但是事情可能看起来不太赞成。我正在得到一个关于扩展名isDirectoryError[Error 21]目录的回溯
。我只想知道如何编写扩展的完整路径。我这里有路径/to/xxxxxxxxxxxxxxxx/1.2.345
我应该把这个版本号放在路径中吗?我希望你能帮忙。我用它来获取扩展的路径,你知道我为什么会遇到这个错误吗?isDirectoryError[错误21]
。提前感谢。有一个问题:是否有一个设置允许您阻止“感谢安装…”选项卡?如果您对一些手动步骤没有意见,这会起作用。对于那些试图完全自动化他们在selenium中所做的事情的人来说,这并不理想。