How to configure ChromeDriver to launch the Chrome browser without head through Selenium?

I am working on a Python script for web scraping and have taken the path of using Chromedriver as one of the packages. I would like this to work in the background without any popups. I use the 'headless' option on chromedriver and it seems to do its job in terms of not showing the browser window, however, I still see the .exe file working. See the screenshot of what I'm talking about. Screenshot

This is the code I use to run 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" 

What I was trying to do was resize the window in the options to 0x0, but I'm not sure I did something, as the .exe file still popped up.

Any ideas how I can do this?

I am using Python 2.7 for your information

+32
source share
4 answers

So, after fixing my code:

 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 appeared when the script started. Although it got rid of some additional output, telling me: "The GPU process failed to start."

As a result, I was running my Python script using a .bat file

So basically

  • Save python script if folder
  • Open a text editor and upload the following code (of course, edit the script)

    c: \ python27 \ python.exe c: \ SampleFolder \ ThisIsMyScript.py% *

  • Save the .txt file and change the extension to .bat

  • Double-click this to run the file.

So, this just opened the script on the command line, and ChromeDriver seems to be working in this window without jumping to the forefront of my screen and thereby solving the problem.

+3
source

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 am sure it will work for 2.7 as well.

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) 
+55
source

When working with Selenium Client 3.11.x, ChromeDriver v2.38 and Google Chrome v65.0.3325.181 in headphone- less mode, the following points should be considered:

  • You need to add the --headless argument to invoke Chrome in headless mode.
  • For Windows systems, you must add the argument --disable-gpu
  • As for Headless: make the --disable-gpu flag unnecessary. --disable-gpu Linux flag --disable-gpu not required on Linux and MacOS systems.
  • Since SwiftShader fails, an offline statement in the --disable-gpu Windows flag will also make the --disable-gpu flag unnecessary on Windows systems.
  • The start-maximized argument is required for a deployed Viewport .
  • You may need to add the --no-sandbox argument to bypass the OS security model.

    • Here is a link to the Sandbox story.
  • Windows code block example:

     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") 
  • Linux code block example:

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

Update (April 23, 2018)

set_headless(headless=True) the Google Chrome browser in set_headless(headless=True) mode has become much easier due to the availability of the set_headless(headless=True) method 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 : the argument --disable-gpu implemented internally.


Update (October 13, 2018)

To now launch the Chrome browser offline, you can simply set the --headless property through the Options() class as follows:

  • Sample code:

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

Outro

How to make Firefox headless programmatically in Selenium using Python?

+33
source
  • .exe will work anyway. According to Google - "Launching in headless mode, i.e. Without depending on the user interface or display server."

  • It is better to add 2 dashes to the command line arguments, i.e. options.add_argument('--headless')

  • In brainless mode, it is also proposed to disable the GPU, i.e. options.add_argument('--disable-gpu')

+4
source

Source: https://habr.com/ru/post/1272863/


All Articles