python - 在 chromedriver 中为 selenium 设置 chrome.prefs 与 python 绑定(bind)

标签 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)

最佳答案

对于任何想要在 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)

关于python - 在 chromedriver 中为 selenium 设置 chrome.prefs 与 python 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15165593/

相关文章:

c# Selenium Chromedriver 打开错误的 url onload

selenium-chromedriver - 无法使用 Selenium 在 Windows 7 中启动 Chrome headless 浏览器

python - 导入 numpy 给出 ImportError : cannot import name TestCase

python - scrapy 调试请求对象

java - 如何向下滚动页面直到出现所有 View ?

c# - 如何在 Selenium 中自动接受 Chrome 的 "Always open these types of links in the associated app"对话框

python - 定义自定义错误消息 Flask Restful API

python - 混合 numpy 矩阵和数组的危险

python - BeautifulSoup不会使用selenium获取页面源

python - 从不同页面获取元素时如何避免 StaleElementReferenceError?