Jab*_*abb 11 python selenium selenium-chromedriver
我一整天都在寻找这个,似乎目前没有针对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)
Run Code Online (Sandbox Code Playgroud)
对于想要在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)
Run Code Online (Sandbox Code Playgroud)
对于其他陷入困境的其他人来说,这只是一个小小的更新。
对于较新的版本,以下代码可以正常工作:
options.add_experimental_option('prefs', {'download.default_directory':'C:\\temp'})
Run Code Online (Sandbox Code Playgroud)
使固定:
有一个解决方案,即避免使用 chromeoptions 对象并恢复到所需功能字典(已弃用)。由于某种原因,selenium 库中的 webdriver.py 向所需功能字典中添加了一个空的 chromeoptions 字典,这使得它变得毫无用处。所以需要取消webdriver.py中第54行的注释
desired_capabilities.update(options.to_capabilities())
Run Code Online (Sandbox Code Playgroud)
然后使用此代码将所有所需的功能传递给 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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
18386 次 |
最近记录: |