Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm working on a python script to web-scrape and have gone down the path of using Chromedriver as one of the packages. I would like this to operate in the background without any pop-up windows. I'm using the option 'headless' on chromedriver and it seems to do the job in terms of not showing the browser window, however, I still see the .exe file running. See the screenshot of what I'm talking about. Screenshot

This is the code I am using to initiate ChromeDriver:

options = webdriver.ChromeOptions() options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"]) options.add_argument('headless') options.add_argument('window-size=0x0') chrome_driver_path = "C:\Python27\Scripts\chromedriver.exe" 

Things I've tried to do is alter the window size in the options to 0x0 but I'm not sure that did anything as the .exe file still popped up.

Any ideas of how I can do this?

I am using Python 2.7 FYI

like image 299
Maz Avatar asked Oct 24 '17 21:10

Maz


People also ask

How do I run ChromeDriver in headless mode?

Post version 59, Chrome supports headless execution. ChromeOptions class is utilized to modify the default characteristics of the browser. The addArguments method of the ChromeOptions class is used for headless execution and headless is passed as a parameter to that method.

How do I open my browser in headless mode?

Which command starts the google chrome web browser in headless mode? As we have already seen, you just have to add the flag –headless when you launch the browser to be in headless mode. With CLI (Command Line Interface), just write: chrome \<br> – headless \ # Runs Chrome in headless mode.

How do I run Selenium in headless browser?

ChromeOptions options = new ChromeOptions() options. addArgument("headless"); ChromeDriver driver = new ChromeDriver(options); In the above code, the browser is instructed to run in the headless mode using the addArgument() method of the ChromeOptions class provided by the Selenium WebDriver.


2 Answers

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, options=options) 
like image 105
Daniel Porteous Avatar answered Sep 18 '22 12:09

Daniel Porteous


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") 

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.

like image 25
undetected Selenium Avatar answered Sep 18 '22 12:09

undetected Selenium