LaVOZs

The World’s Largest Online Community for Developers

'; Set chrome.prefs with python binding for selenium in chromedriver - LavOzs.Com

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)

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)

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'})

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")
user494599

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)

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')
Related
Calling an external command from Python
What are metaclasses in Python?
Is there a way to run Python on Android?
Finding the index of an item in a list
Does Python have a ternary conditional operator?
How to get the current time in Python
Manually raising (throwing) an exception in Python
Does Python have a string 'contains' substring method?