如何选择使用Selenium时启用的Chrome扩展程序[重复]

时间:2022-08-26 21:32:03

This question already has an answer here:

这个问题在这里已有答案:

I am using Selenium web driver to develop an automated test using Chrome as my browser. I am using Python for this.

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

I have an extension on my Chrome browser that I would like enabled when Selenium opens Chrome. The problem is that when Selenium opens Chrome all the extensions are disabled by default.

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

How do I enable all or a certain extension on the Chrome browser when Selenium runs?

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

1 个解决方案

#1


You can accomplish this using ChromeOptions class or DesiredCapabilities. For that you have to have the .crx file and load that with driver instance.

您可以使用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://*.com")
driver.quit()

Code taken from @alecxe answer here and more details about ChromeOptions and DesiredCapabilities here

来自@alecxe的代码在此处回答以及有关ChromeOptions和DesiredCapabilities的更多详细信息

#1


You can accomplish this using ChromeOptions class or DesiredCapabilities. For that you have to have the .crx file and load that with driver instance.

您可以使用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://*.com")
driver.quit()

Code taken from @alecxe answer here and more details about ChromeOptions and DesiredCapabilities here

来自@alecxe的代码在此处回答以及有关ChromeOptions和DesiredCapabilities的更多详细信息