Selenium: How can I find the correct command for add_arguments()?

Viewed 1304

I am using selenium python and chrome driver and wanted to know if there is any way you can get the command to enable or disable options in add_arguments() function. For example, there are '--disable-infobars', etc., but if I come across a new setting, how do I find its appropriate command? An example being the settings to auto-download pdfs. Any help is appreciated.

2 Answers

Chromium has lots of command switches, such as --disable-extensions or --disable-popup-blocking that can be enabled at runtime using Options().add_argument()

Here is a list of some of the Chromium Command Line Switches.

Chromium also allows for other runtime enabled features, such as useAutomationExtension or plugins.always_open_pdf_externally. These features are enabled using DesiredCapabilities.

I normal review the source code for Chromium when I need to find find other features to control with DesiredCapabilities.

The code below uses both command switches and runtime enabled features to automatically save a PDF file to disk without being prompted.

For my answer I downloaded a PDF file from the Library of Congress.

If you have any questions related to this code or something else related to your question please let me know.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

chrome_options = Options()
chrome_options.add_argument('--disable-infobars')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--disable-popup-blocking')

# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])


# you can set the path for your download_directory
prefs = {
    'download.default_directory': 'download_directory',
    'download.prompt_for_download': False,
    'plugins.always_open_pdf_externally': True
}

capabilities = DesiredCapabilities().CHROME
chrome_options.add_experimental_option('prefs', prefs)
capabilities.update(chrome_options.to_capabilities())

driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)

url_main = 'https://www.loc.gov/aba/publications/FreeLCC/freelcc.html'

driver.get(url_main)

driver.implicitly_wait(20)

download_pdf_file = driver.find_element_by_xpath('//*[@id="main_body"]/ul[2]/li[1]/a')
download_pdf_file.click()

You can add options arguments to a chromium webdriver using Python the following way:

    options = webdriver.ChromeOptions()

    # Arguments go below
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-gpu")
    options.add_argument("--window-size=800,600")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--user-agent={}".format("your user agent string"))
    # Etc etc..

    options.binary_location = "absolute/path/to/chrome.exe"
    driver = webdriver.Chrome(
        desired_capabilities=caps,
        executable_path="absolute/path/to/chromium-driver.exe",
        options=options,
    )

Here you can find the list of all the supported arguments for chrome.

Related