How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium?
It should look like this:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = 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 webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.headless = True
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
Answer update of 13-October-2018
To initiate a google-chrome-headless 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 webdriver from selenium.webdriver.chrome.options import Options options = Options() options.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()
Answer update of 23-April-2018
Invoking google-chrome in headless 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 webdriver from selenium.webdriver.chrome.options import Options options = 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 windows code block :
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("--headless") # Runs Chrome in headless mode. options.add_argument('--no-sandbox') # Bypass OS security model options.add_argument('--disable-gpu') # applicable to windows os only options.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 linux code block :
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("--headless") # Runs Chrome in headless mode. options.add_argument('--no-sandbox') # # Bypass OS security model options.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")
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!
from selenium.webdriver.support.ui import WebDriverWait
chrome_options = webdriver.ChromeOptions()
chrome_options.headless = True
self.driver = webdriver.Chrome(
executable_path=DRIVER_PATH, chrome_options=chrome_options)