How to Drive Headless Chrome with Python

In this lesson we want to learn How to Drive Headless Chrome with Python, Driving Headless Chrome with Python can be done using the Selenium library. Selenium is popular open source framework for automating web browsers and it supports driving Chrome in headless mode, which means that browser runs in the background without graphical user interface.

To drive Headless Chrome with Python using Selenium, you first need to install Selenium and the ChromeDriver which is separate executable that allows Selenium to interact with Chrome. After that you can write a Python script that uses the Selenium API to control Chrome just as if you were controlling it manually.

this is an example that opens a URL in Headless Chrome using Selenium and Python:

In this example first we have created an instance of ChromeOptions and added the –headless and –disable-gpu arguments to start Chrome in headless mode and then create an instance of the ChromeDriver, passing the options to it. After that we have used the get method to open URL, and the title attribute to get the title of the page. Finally, we call the quit method to close the browser.

 

Note: make sure that you have installed selenium using this command pip install selenium

 

Learn More on Python

Leave a Comment