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:
1 2 3 4 5 6 7 8 9 10 |
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--disable-gpu') browser = webdriver.Chrome(options=options) browser.get('https://www.google.com') print(browser.title) browser.quit() |
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
- How to Add Matplotlib in TKinter Window
- How to Make an Instagram Bot
- How to send Message to Instagram with Python
- TKinter Application with Dialogs
- Build Multi Window Application with TKinter
- How to Build Charts in TKinter
- How to Install TKinter in Windows and Linux
- How to Use ChatGPT in Python
- Which Websites are Made with Django