How do I pass options to the Selenium Chrome driver using Python?
Asked Answered
M

6

100

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.

Modulator answered 2/10, 2012 at 21:45 Comment(0)
M
150

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)
Modulator answered 2/10, 2012 at 21:45 Comment(3)
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")Chide
Note: chrome_options arg is now deprecated in favor of the simpler options, e.g.: driver = webdriver.Chrome(options=chrome_options)Ultraviolet
Hey, @Modulator 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!Ballad
H
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)
Hindi answered 29/2, 2016 at 9:37 Comment(0)
T
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)
Thayne answered 9/8, 2016 at 15:4 Comment(0)
B
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.

Bacon answered 5/3, 2019 at 16:14 Comment(0)
P
3

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')
Pamper answered 30/5, 2023 at 11:19 Comment(0)
S
0

This is how I did it as I needed a matching version of the Chrome. First, make sure you install a supported version of the Chrome. Go to the webdriver page and mark the latest supported version of your chrome. Then run (replace with your version)

npx @puppeteer/browsers install [email protected]

Then, install the auto-installer and the following should work out of the box:

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

options = Options()
options.add_argument('--headless=new')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)
try:
    driver.get("https://www.google.com")
    print("Page title was '{}'".format(driver.title))
finally:
    driver.quit()
Sync answered 31/10, 2023 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.