Skip to content
Advertisement

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:Python27Scriptschromedriver.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

Advertisement

Answer

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:Python27Scriptschromedriver.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:python27python.exe c:SampleFolderThisIsMyScript.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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement