我正在使用 Selenium Web 驱动程序使用 Chrome 作为浏览器来开发自动化测试。我为此使用Python。
我的 Chrome 浏览器上有一个扩展,我希望在 Selenium 打开 Chrome 时启用该扩展。问题是,当 Selenium 打开 Chrome 时,默认情况下所有扩展程序都被禁用。
当 Selenium 运行时,如何在 Chrome 浏览器上启用所有或特定扩展?
您可以使用以下方法来完成此操作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()
代码取自@alecxe 答案here https://stackoverflow.com/questions/16511384/using-extensions-with-selenium-python以及有关 ChromeOptions 和 DesiredCapability 的更多详细信息here https://sites.google.com/a/chromium.org/chromedriver/capabilities
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)