Photo by Diego Molina (diemol) / CC BY-SA 4.0
Mac OS Big Sur 11.1
ChromeDriver Official Sample Code
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
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
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()