14

I have been searching all day for this and it seems that there is no solution currently available from the chromedriver implementation for python.

how do you set specific chrome.prefs (for example profile settings such as profile.managed_default_content_settings.images = 2) using the webdriver.Chrome() method?

I already tried it through webdriver.ChromeOptions() without success. In Java there are appropriate functions available to achieve this.

But Python? This is what I am doing currently...

    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)
Jabb
  • 3,414
  • 8
  • 35
  • 58

5 Answers5

9

For anyone who want to disable images in chromedriver, the following code might help you.

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)
Xiong
  • 617
  • 1
  • 8
  • 12
7

Just a small update for everybody else stumbling over this question.

For newer versions the following code works without problems:

options.add_experimental_option('prefs', {'download.default_directory':'C:\\temp'})
eric
  • 1,857
  • 5
  • 23
  • 33
4

This is what works with latest chromedriver versions from at least 2.15 to the current version 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")
3

Fix:

There is a solution by avoiding the chromeoptions object and reverting back to the desiredcapabilities dictionary (deprecated). For some reason webdriver.py in the selenium library adds an empty chromeoptions dictionary to the desiredcapabilities dictionary which renders it useless. So you need to uncomment line 54 in webdriver.py

desired_capabilities.update(options.to_capabilities())

Then use this code to pass all desired capabilities to 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)
Jabb
  • 3,414
  • 8
  • 35
  • 58
  • Similar issue here (I'm trying to change Chrome's download folder). Tried your code, but somehow it's not working for me. The `desired_capabilities.update(options.to_capabilities())` line on my webdriver.py file was not commented out. Any thoughts? Have you come across any other solutions? – Parzival Aug 03 '13 at 00:22
  • nope, it was just that. did you find this line? desired_capabilities.update(options.to_capabilities()) – Jabb Aug 26 '13 at 14:19
  • I did. It was not commented out, so it should have worked, but somehow it didn't. In the end I gave up on Chrome altogether. – Parzival Aug 26 '13 at 15:33
  • Thanks! If `caps['chromeOptions']` exists, `caps['chrome.prefs']` will be ignored. Subclass `ChromeOptions` and override `to_capabilities` make it work. – youfu Feb 06 '14 at 17:30
  • @zhangyoufu exactly! that's a way to accomplish it! – Jabb Feb 06 '14 at 18:03
3

For anyone struggling with the Python syntax, here's a complete, working example. It disables Chrome's "Do you want Google Chrome to save your password for this site?" prompt. See also 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')
Community
  • 1
  • 1
Steve Saporta
  • 4,581
  • 3
  • 30
  • 32