如何选择Chrome扩展程序以在使用Selenium时启用

Sky*_*lue 5 python selenium webdriver web-testing google-chrome-extension

我使用Selenium网络驱动程序开发使用Chrome作为浏览器的自动化测试.我正在使用Python.

当Selenium打开Chrome时,我的Chrome浏览器上有一个扩展程序.问题是,当Selenium打开Chrome时,默认情况下会禁用所有扩展程序.

当Selenium运行时,如何在Chrome浏览器上启用所有或某个扩展程序?

Sai*_*fur 4

您可以使用ChromeOptions类或来完成此操作DesiredCapabilities。为此,您必须拥有该.crx文件并使用驱动程序实例加载该文件。

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()
Run Code Online (Sandbox Code Playgroud)

代码取自 @alecxe 答案这里以及有关 ChromeOptions 和 DesiredCapability 的更多详细信息这里