如何选择Chrome扩展程序以在使用Selenium时启用 [英] How to select Chrome extensions to enable when using Selenium

查看:1435
本文介绍了如何选择Chrome扩展程序以在使用Selenium时启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的Chrome浏览器上有一个扩展,我希望在Selenium打开Chrome时启用它。问题是,当Selenium打开Chrome浏览器时,默认情况下所有扩展功能都是禁用的。



在Selenium运行时,如何在Chrome浏览器中启用全部或特定扩展程序? ChromeOptions 类或来完成此操作。

解决方案 DesiredCapabilities 。为此,您必须具有 .crx 文件并使用驱动程序实例加载该文件。

 从selenium导入os 
从selenium.webdriver.chrome.options导入webdriver
导入选项


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答案此处以及有关ChromeOptions和DesiredCapabilities的更多详细信息此处


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

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.

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

解决方案

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

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

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

这篇关于如何选择Chrome扩展程序以在使用Selenium时启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆