设置chrome.prefs与python绑定硒在chromedriver
我一直在寻找这一整天,似乎没有解决方案目前从python的chromedriver实现可用。设置chrome.prefs与python绑定硒在chromedriver
如何使用webdriver.Chrome()方法设置特定的chrome.prefs(例如profile.managed_default_content_settings.images = 2等配置文件设置)?
我已经通过webdriver.ChromeOptions()试过了,但没有成功。在Java中,有适当的功能可以实现这一点。
但是Python?这是我在做什么目前...
options = webdriver.ChromeOptions()
options.add_argument('--allow-running-insecure-content')
options.add_argument('--disable-web-security')
options.add_argument('--disk-cache-dir=/var/www/cake2.2.4/app/tmp/cache/selenium-chrome-cache')
options.add_argument('--no-referrers')
options.add_argument('--window-size=1003,719')
options.add_argument('--proxy-server=localhost:8118')
options.add_argument("'chrome.prefs': {'profile.managed_default_content_settings.images': 2}")
self.selenium = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver',chrome_options=options)
修复:
有通过避免chromeoptions对象,并回复到desiredcapabilities字典的解决方案(不推荐)。出于某种原因,selenium库中的webdriver.py将一个空的chromeoptions字典添加到desiredcapabilities字典中,使其无用。所以,你需要取消注释行54 webdriver.py
desired_capabilities.update(options.to_capabilities())
然后使用此代码通过所有所需的功能,以chromedriver
CHROME = {
"browserName": "chrome",
"version": "",
"platform": "ANY",
"javascriptEnabled": True,
"chrome.prefs": {"profile.managed_default_content_settings.images": 2},
"proxy": {
"httpProxy":"localhost:8118",
"ftpProxy":None,
"sslProxy":None,
"noProxy":None,
"proxyType":"MANUAL",
"class":"org.openqa.selenium.Proxy",
"autodetect":False
},
"chrome.switches": ["window-size=1003,719", "allow-running-insecure-content", "disable-web-security", "disk-cache-dir=/var/www/cake2.2.4/app/tmp/cache/selenium-chrome-cache", "no-referrers"],
}
self.selenium = webdriver.Chrome(desired_capabilities=CHROME)
只是一个小更新别人绊倒这个问题。
对于较新版本的下面的代码工作没有问题:
options.add_experimental_option('prefs', {'download.default_directory':'C:\\temp'})
对于谁想要禁用chromedriver图像的人,下面的代码可能会帮助你。
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("prefs", {'profile.default_content_settings.images': 2})
driver = webdriver.Chrome(chrome_options=chrome_options)
这与至少2.15最新版本chromedriver到当前版本2.20是什么在起作用:
chrome_options = Options()
chrome_options.add_experimental_option("prefs", {'profile.managed_default_content_settings.images': 2})
chrome = webdriver.Chrome('/path/to/chromedriver',chrome_options=chrome_options)
chrome.get("https://google.com")
对于任何与Python语法挣扎,这里有一个完整,工作示例。它禁用了Chrome的“您希望Google Chrome为您的网站保存密码吗?”提示。另见WebDriver Chrome Browser: Avoid 'Do you want chrome to save your password' pop up。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option('prefs', {
'credentials_enable_service': False,
'profile': {
'password_manager_enabled': False
}
})
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://google.com')
类似的问题在这里(我试图改变Chrome的下载文件夹)。尝试了你的代码,但不知何故,它不适合我。我的webdriver.py文件中的desired_capabilities.update(options.to_capabilities())行没有被注释掉。有什么想法吗?你有没有遇到过其他解决方案? – Parzival 2013-08-03 00:22:20
不,这只是。你觉得这行吗? desired_capabilities.update(options.to_capabilities()) – Jabb 2013-08-26 14:19:12
我做过。它没有被注释掉,所以它应该有效,但不知道它没有。最后,我完全放弃了Chrome。 – Parzival 2013-08-26 15:33:53