Set chrome.prefs with python binding for selenium in chromedriver

前端 未结 5 1167
我寻月下人不归
我寻月下人不归 2020-12-04 22:37

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 spe

相关标签:
5条回答
  • 2020-12-04 23:17

    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)
    
    0 讨论(0)
  • 2020-12-04 23:24

    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'})
    
    0 讨论(0)
  • 2020-12-04 23:29

    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')
    
    0 讨论(0)
  • 2020-12-04 23:34

    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")
    
    0 讨论(0)
  • 2020-12-04 23:41

    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)
    
    0 讨论(0)
提交回复
热议问题