简体   繁体   English

如何在 Selenium Python 中设置 Chrome 的首选项

[英]How to set preferences for Chrome in Selenium Python

I am able to set preferences for Firefox as below.我可以如下设置 Firefox 的首选项。

set_preference = profile.set_preference
set_preference("network.http.response.timeout", 30)
set_preference("media.autoplay.enabled", False)
set_preference("browser.cache.memory.enable", False)
set_preference("browser.cache.disk.enable", False)
set_preference("network.proxy.type", 2)
set_preference("network.proxy.autoconfig_url", pac_url)
set_preference("network.proxy.autoconfig_url.include_path", True)

But I need to setup for Chrome as well.. Can any one assist me how to do?但我也需要为 Chrome 设置.. 有人可以帮我怎么做吗?

Thanks Hafsa.谢谢哈夫萨。

For Chrome, I think you are looking for ChromeOptions here.对于 Chrome,我认为您正在这里寻找ChromeOptions You can add prefs to the ChromeOptions .您可以将首ChromeOptions prefs

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# options
options = Options()
options.add_argument("--disable-extensions")
options.add_argument("--disable-infobars")
options.add_argument("--headless")
# etc...

# declare prefs
prefs = {"media.autoplay.enabled" : False, "network.proxy.autoconfig_url" : pac_url, "network.proxy.autoconfig_url.include_path" : True}

# add prefs 
chromeOptions.add_experimental_option("prefs", prefs)


driver = webdriver.Chrome(chrome_options=options)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM