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

硒文档提到 Chrome 网络驱动程序可以采用 ChromeOptions的一个实例,但是我不知道如何创建 ChromeOptions

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

164452 次浏览

Found the chrome Options class in the Selenium source code.

Usage to create a Chrome driver instance:

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)

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)

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

desired_capabilities['chromeOptions'] = {
"args": ["--disable-extensions"],
"extensions": []
}
webdriver.Chrome(desired_capabilities=desired_capabilities)
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_capabilities and options.to_capabilities() are dictionaries. You can use the dict.update() method to add the options to the main set.