如何使用 Python 将选项传递给 Selenium Chrome 驱动程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12698843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:37:38  来源:igfitidea点击:

How do I pass options to the Selenium Chrome driver using Python?

pythongoogle-chromeseleniumselenium-chromedriver

提问by k107

The Selenium documentationmentions that the Chrome webdriver can take an instance of ChromeOptions, but I can't figure out how to create ChromeOptions.

硒的文件中提到,在Chrome的webdriver可以采取的一个实例ChromeOptions,但我无法弄清楚如何创建ChromeOptions

I'm hoping to pass the --disable-extensionsflag to Chrome.

我希望将--disable-extensions标志传递给 Chrome。

回答by k107

Found the chrome Options class in the Selenium source code.

在 Selenium 源代码中找到了chrome Options 类

Usage to create a Chrome driver instance:

用于创建 Chrome 驱动程序实例:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=chrome_options)

回答by Hassan Raza

This is how I did it.

我就是这样做的。

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-extensions')

chrome = webdriver.Chrome(chrome_options=chrome_options)

回答by Andriy Ivaneyko

Code which disable chrome extensions for ones, who uses DesiredCapabilitiesto set browser flags :

为使用DesiredCapabilities设置浏览器标志的人禁用 chrome 扩展的代码:

desired_capabilities['chromeOptions'] = {
    "args": ["--disable-extensions"],
    "extensions": []
}
webdriver.Chrome(desired_capabilities=desired_capabilities)

回答by user3389572

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--disable-logging')

# Update your desired_capabilities dict withe extra options.
desired_capabilities.update(options.to_capabilities())
driver = webdriver.Remote(desired_capabilities=options.to_capabilities())

Both the desired_capabilitiesand options.to_capabilities()are dictionaries. You can use the dict.update()method to add the optionsto the main set.

无论是desired_capabilitiesoptions.to_capabilities()的字典。您可以使用dict.update()方法将选项添加到主集。