close

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

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

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

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

    The .exe file still came up when running the script. Although this did get rid of some extra output telling me “Failed to launch GPU process”.

  2. configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium

    The .exe file still came up when running the script. Although this did get rid of some extra output telling me “Failed to launch GPU process”.

Method 1

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)

Method 2

So after correcting my code to:

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

The .exe file still came up when running the script. Although this did get rid of some extra output telling me “Failed to launch GPU process”.

What ended up working is running my Python script using a .bat file

So basically,

  1. Save python script if a folder
  2. Open text editor, and dump the following code (edit to your script of course)c:\python27\python.exe c:\SampleFolder\ThisIsMyScript.py %*
  3. Save the .txt file and change the extension to .bat
  4. Double click this to run the file

So this just opened the script in Command Prompt and ChromeDriver seems to be operating within this window without popping out to the front of my screen and thus solving the problem.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read