使用 Selenium 扩展 (Python)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16511384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:52:10  来源:igfitidea点击:

Using Extensions with Selenium (Python)

pythongoogle-chromeseleniumselenium-webdriver

提问by Michael Wu

I am currently using Selenium to run instances of Chrome to test web pages. Each time my script runs, a clean instance of Chrome starts up (clean of extensions, bookmarks, browsing history, etc). I was wondering if it's possible to run my script with Chrome extensions. I've tried searching for a Python example, but nothing came up when I googled this.

我目前正在使用 Selenium 运行 Chrome 实例来测试网页。每次我的脚本运行时,都会启动一个干净的 Chrome 实例(清除扩展程序、书签、浏览历史记录等)。我想知道是否可以使用 Chrome 扩展程序运行我的脚本。我试过搜索一个 Python 示例,但是当我用谷歌搜索这个时什么也没有出现。

采纳答案by alecxe

You should use Chrome WebDriver optionsto set a list of extensions to load. Here's an example:

您应该使用 Chrome WebDriver选项来设置要加载的扩展程序列表。下面是一个例子:

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()

Hope that helps.

希望有帮助。

回答by r3robertson

The leading answer didn't work for me because I didn't realize you had to point the webdriver options toward a .zipfile.

领先的答案对我不起作用,因为我没有意识到您必须将 webdriver 选项指向.zip文件。

I.e. chrome_options.add_extension('path_to_extension_dir')doesn't work.
You need: chrome_options.add_extension('path_to_extension_dir.zip')

chrome_options.add_extension('path_to_extension_dir')不起作用。
你需要:chrome_options.add_extension('path_to_extension_dir.zip')

After figuring that out and reading a couplepostson how to create the zip file via the command line and load it into selenium, the only way it worked for me was to zip my extension files within the same python script. This actually turned out to be a nice way for automatically updating any changes you might have made to your extension:

搞清楚了这一点,并阅读后夫妇的帖子关于如何创建通过命令行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()

回答by Khan Saad

If you wanna import any chrome extension in your selenium python scrip

如果您想在 selenium python 脚本中导入任何 chrome 扩展

  1. Put your extension.crx.crx file in the same folder as your code or give the path

  2. you can copy-paste this code and just change the file crx.crx name

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

    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")
    
  1. 将您的 extension.crx.crx 文件与您的代码放在同一文件夹中或提供路径

  2. 您可以复制粘贴此代码并更改文件 crx.crx 名称

    从 selenium 导入 o​​s 从 selenium.webdriver.chrome.options 导入 webdriver 导入选项

    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")
    

if this code is throwing an error maybe thiswill solve it

如果此代码抛出错误,也许会解决它

回答by gg-dev-05

I also needed to add an extention to chrome while using selenium. What I did was first open the browser using selenium then add extention to the browser in the normal way like you would do in google chrome.

我还需要在使用 selenium 时为 chrome 添加一个扩展。我所做的是首先使用 selenium 打开浏览器,然后像在 google chrome 中一样以正常方式向浏览器添加扩展。