How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? python python

How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?


It should look like this:

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsoptions = Options()options.add_argument('--headless')options.add_argument('--disable-gpu')  # Last I checked this was necessary.driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)

This works for me using Python 3.6, I'm sure it'll work for 2.7 too.

Update 2018-10-26: These days you can just do this:

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsoptions = Options()options.headless = Truedriver = webdriver.Chrome(CHROMEDRIVER_PATH, options=options)


Answer update of 13-October-2018

To initiate a browsing context using Selenium driven ChromeDriver now you can just set the --headless property to true through an instance of Options() class as follows:

  • Effective code block:

    from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsoptions = Options()options.headless = Truedriver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')driver.get("http://google.com/")print ("Headless Chrome Initialized")driver.quit()

Answer update of 23-April-2018

Invoking in mode programmatically have become much easier with the availability of the method set_headless(headless=True) as follows :

  • Documentation :

    set_headless(headless=True)    Sets the headless argument    Args:        headless: boolean value indicating to set the headless option
  • Sample Code :

    from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsoptions = Options()options.set_headless(headless=True)driver = webdriver.Chrome(options=options, executable_path=r'C:\path\to\chromedriver.exe')driver.get("http://google.com/")print ("Headless Chrome Initialized")driver.quit()

Note : --disable-gpu argument is implemented internally.


Original Answer of Mar 30 '2018

While working with Selenium Client 3.11.x, ChromeDriver v2.38 and Google Chrome v65.0.3325.181 in Headless mode you have to consider the following points :

  • You need to add the argument --headless to invoke Chrome in headless mode.

  • For Windows OS systems you need to add the argument --disable-gpu

  • As per Headless: make --disable-gpu flag unnecessary --disable-gpu flag is not required on Linux Systems and MacOS.

  • As per SwiftShader fails an assert on Windows in headless mode --disable-gpu flag will become unnecessary on Windows Systems too.

  • Argument start-maximized is required for a maximized Viewport.

  • Here is the link to details about Viewport.

  • You may require to add the argument --no-sandbox to bypass the OS security model.

  • Effective code block :

    from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsoptions = Options()options.add_argument("--headless") # Runs Chrome in headless mode.options.add_argument('--no-sandbox') # Bypass OS security modeloptions.add_argument('--disable-gpu')  # applicable to windows os onlyoptions.add_argument('start-maximized') # options.add_argument('disable-infobars')options.add_argument("--disable-extensions")driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\path\to\chromedriver.exe')driver.get("http://google.com/")print ("Headless Chrome Initialized on Windows OS")
  • Effective code block :

    from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsoptions = Options()options.add_argument("--headless") # Runs Chrome in headless mode.options.add_argument('--no-sandbox') # # Bypass OS security modeloptions.add_argument('start-maximized')options.add_argument('disable-infobars')options.add_argument("--disable-extensions")driver = webdriver.Chrome(chrome_options=options, executable_path='/path/to/chromedriver')driver.get("http://google.com/")print ("Headless Chrome Initialized on Linux OS")

Steps through YouTube Video

How to initialize Chrome Browser in Maximized Mode through Selenium

Outro

How to make firefox headless programmatically in Selenium with python?


tl; dr

Here is the link to the Sandbox story.


Update August 20, 2020 -- Now is simple!

chrome_options = webdriver.ChromeOptions()chrome_options.headless = Trueself.driver = webdriver.Chrome(            executable_path=DRIVER_PATH, chrome_options=chrome_options)