In this tutorial, we will introduce how to enable chrome headless mode in python selenium, which is helpful if you do not want to see chrome browser gui.
To understand what is chrome headless mode, you can see:
An Introduction to Google Chrome Headless Mode – Chrome Tutorial
How to enable chrome headless mode in selenium?
We can add some options for selenium. Here is an example code:
from selenium import webdriver import selenium url = 'https://www.tutorialexample.com/' driver = None try: options = selenium.webdriver.chrome.options.Options() options.set_headless(True) driver = webdriver.Chrome(executable_path=r"chromedriver.exe", options = options) driver.get(url) #img image_list = driver.find_elements_by_tag_name("img") print(image_list) for img in image_list: print(img) except Exception as e: print("exception = ",e) finally: if driver is not None: driver.quit()
In this code, we use an options to enable chrome headless mode.
options = selenium.webdriver.chrome.options.Options() options.set_headless(True)
Then, we can use this options to start chrome browser.
driver = webdriver.Chrome(executable_path=r”chromedriver.exe”, options = options)
Run this code, we will start a chrome browser without chrome gui. We may see:
<selenium.webdriver.remote.webelement.WebElement (session="61ac5c18f87f126f46155a3f817776de", element="15705467-928e-4e82-ae07-edb69e4eb9ef")> <selenium.webdriver.remote.webelement.WebElement (session="61ac5c18f87f126f46155a3f817776de", element="5fede4f7-b442-4f0d-a6bb-ad2db51c21e6")> <selenium.webdriver.remote.webelement.WebElement (session="61ac5c18f87f126f46155a3f817776de", element="8fedc456-ebc8-49d8-ad20-a311b9479251")> <selenium.webdriver.remote.webelement.WebElement (session="61ac5c18f87f126f46155a3f817776de", element="1ad84f52-fb60-4ed1-9624-a99f12e22b53")>