Search code examples
pythonseleniumgoogle-chrome

How to set preferences for Chrome in Selenium Python


I am able to set preferences for Firefox as below.

set_preference = profile.set_preference
set_preference("network.http.response.timeout", 30)
set_preference("media.autoplay.enabled", False)
set_preference("browser.cache.memory.enable", False)
set_preference("browser.cache.disk.enable", False)
set_preference("network.proxy.type", 2)
set_preference("network.proxy.autoconfig_url", pac_url)
set_preference("network.proxy.autoconfig_url.include_path", True)

But I need to setup for Chrome as well.. Can any one assist me how to do ?

Thanks Hafsa.


Solution

  • For Chrome, I think you are looking for ChromeOptions here. You can add prefs to the ChromeOptions.

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    # options
    options = Options()
    options.add_argument("--disable-extensions")
    options.add_argument("--disable-infobars")
    options.add_argument("--headless")
    # etc...
    
    # declare prefs
    prefs = {"media.autoplay.enabled" : False, "network.proxy.autoconfig_url" : pac_url, "network.proxy.autoconfig_url.include_path" : True}
    
    # add prefs 
    options.add_experimental_option("prefs", prefs)
    
    
    driver = webdriver.Chrome(chrome_options=options)