python - 将扩展与 Selenium (Python) 结合使用

标签 python google-chrome selenium selenium-webdriver

我目前正在使用 Selenium 运行 Chrome 实例来测试网页。每次我的脚本运行时,都会启动一个干净的 Chrome 实例(清除扩展、书签、浏览历史等)。我想知道是否可以使用 Chrome 扩展程序运行我的脚本。我曾尝试搜索一个 Python 示例,但是当我用谷歌搜索时没有找到任何结果。

最佳答案

您应该使用 Chrome WebDriver 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()

希望对您有所帮助。

关于python - 将扩展与 Selenium (Python) 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16511384/

相关文章:

python - SMTP 邮件客户端 Python

c# - System.InvalidOperationException : unknown error: Runtime. 评估抛出异常:通过 C# 使用 Azure 和 Selenium 的 DOMException 错误

javascript - 多次点击 Protractor 不起作用

google-chrome - Twitter按钮未在chrome中显示推文计数

selenium - 如何使 Xvfb 显示可见?

selenium - 使用WebdriverJS时如何访问错误?

python - 用python更改目录

Python 程序,计算掷公平骰子 12 次时至少两次获得 1 的概率

python - 正则表达式从字符串中过滤版本

jquery - class a :hover working for text-decoration, 但不是颜色