EnglishTechPython

[Python][Selenium]How to launch WebDriver with Headless Chrome

English

If you are going to use Selenium against a dynamic web site such as using Javascript, you should use Headless Chrome. However, it seems that there are no helpful instruction so far. So I’m going to explain how to launch WebDriver with Headless Chrome.

Photo by Diego Molina (diemol)CC BY-SA 4.0

Environment

[ads]

Mac OS Big Sur 11.1
Python 3.8.3
ChromeDriver 89.0.4389.23

ChromeDriver Official Sample Code

[ads]

I found the official sample code from ChromeDriver Official Site, however, there are no explanation about “Headless”. Here is the sample code cited from this site.

mport time
from selenium import webdriver

driver = webdriver.Chrome('/path/to/chromedriver')  # Optional argument, if not specified will search path.
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()

Add options parameters

[ads]

What we have to do to launch with Headless Chrome is to add “options” parameters as following. Note that the launch parameter appeared in the 3rd line(‘/usr/local/bin/chromedriver’) should be customized as your own valid path.

options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=options)

Sample code that enable to launch with Headless Chrome

[ads]
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.keys import Keys

options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=options)

driver.get('http://www.google.com/')
# your process using driver

driver.quit()

References

Ads