95

The Selenium documentation mentions that the Chrome webdriver can take an instance of ChromeOptions, but I can't figure out how to create ChromeOptions.

I'm hoping to pass the --disable-extensions flag to Chrome.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
k107
  • 15,882
  • 11
  • 61
  • 59

5 Answers5

147

Found the chrome Options class in the Selenium source code.

Usage to create a Chrome driver instance:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=chrome_options)
congusbongus
  • 13,359
  • 7
  • 71
  • 99
k107
  • 15,882
  • 11
  • 61
  • 59
  • 7
    This answer was a lifesaver. In case it's useful to others, to enable ES6 Harmony features, the call is `chrome_options.add_argument("--js-flags=--harmony")` – msridhar Jun 18 '14 at 00:04
  • 16
    Note: `chrome_options` arg is now deprecated in favor of the simpler `options`, e.g.: `driver = webdriver.Chrome(options=chrome_options)` – Mike B Jan 31 '19 at 23:29
  • Hey, @k107 I was wondering If I can do the same thing except for one change. Can I use `chrome_options.add_argument("--enable-extensions")` to enable all **extensions** instead of adding each extension manually by (code)? Thanks in advance! – Joe Sep 08 '19 at 21:00
16

This is how I did it.

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-extensions')

chrome = webdriver.Chrome(chrome_options=chrome_options)
Hassan Raza
  • 3,025
  • 22
  • 35
7

Code which disable chrome extensions for ones, who uses DesiredCapabilities to set browser flags :

desired_capabilities['chromeOptions'] = {
    "args": ["--disable-extensions"],
    "extensions": []
}
webdriver.Chrome(desired_capabilities=desired_capabilities)
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
5
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('--disable-logging')

# Update your desired_capabilities dict withe extra options.
desired_capabilities.update(options.to_capabilities())
driver = webdriver.Remote(desired_capabilities=options.to_capabilities())

Both the desired_capabilities and options.to_capabilities() are dictionaries. You can use the dict.update() method to add the options to the main set.

user3389572
  • 381
  • 3
  • 5
1

If you use regular chromedriver:

pip3 install selenium

Example code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def main():
    # Set the path to the chromedriver
    chromedriver_path = "path/to/chromedriver"

    # Create ChromeOptions
    options = Options()

    # Your Options here....
    # options.add_argument("--headless")  # Run in headless mode
    # options.add_argument("--disable-gpu")  # Disable GPU acceleration

    # Create a ChromeDriver
    driver = webdriver.Chrome(executable_path=chromedriver_path, options=options)

    # Open Website
    driver.get("https://www.google.com")

    # your code.. clicking on buttons etc..

    # Close the ChromeDriver
    driver.quit()

if __name__ == "__main__":
    main()

If you use undetected chromedriver:

pip3 install undetected-chromedriver

Example code:

import undetected_chromedriver as uc


def main():
    # Set the path to the chromedriver executable
    chromedriver_path = "path/to/chromedriver"

    # Create options object
    options = uc.ChromeOptions()

    # Add options if needed
    # options.add_argument("--headless")  # Run in headless mode
    # options.add_argument("--disable-gpu")  # Disable GPU acceleration

    # Create an instance of Undetected ChromeDriver with options
    driver = uc.Chrome(executable_path=chromedriver_path, options=options)

    try:
        # Open Google
        driver.get("https://www.google.com")

        # Rest of your code for interacting with the Google page

    except Exception as ex:
        print(ex)

    finally:
        # Close the ChromeDriver
        driver.quit()

if __name__ == "__main__":
    main()

Here some of the chrome options I use regularly:

    options.add_argument(f"--window-size=1366,768")
    options.add_argument(f'--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36')
    options.add_argument('--disable-blink-features=AutomationControlled')
    options.add_argument("--disable-extensions")
    options.add_argument("--proxy-server='direct://'")
    options.add_argument("--proxy-bypass-list=*")
    options.add_argument('--ignore-certificate-errors')
    options.add_argument("--password-store=basic")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-extensions")
    options.add_argument("--enable-automation")
    options.add_argument("--disable-browser-side-navigation")
    options.add_argument("--disable-web-security")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-infobars")
    options.add_argument("--disable-gpu")
    options.add_argument("--disable-setuid-sandbox")
    options.add_argument("--disable-software-rasterizer")

    options.add_argument(f"--user-data-dir=PATH_TO_CHROME_PROFILE")
    options.add_argument('--proxy-server=IP_ADRESS:PORT')
kit4py
  • 48
  • 8