Running headless Chrome with Selenium in Python

pyzzled
1 min readAug 13, 2017

--

As of Chrome 60 (59 for Mac and Linux users), Headless Chrome is available for you to play with.

What do we want to do?

Let’s launch Chrome in headless mode, hit the Google homepage, click the I’m Feeling Lucky button and take a screenshot of the result.

Getting Started

First, let’s import everything we’ll need to run Chrome in headless mode using Selenium.

Defining Chrome

Before we set up a Chrome webdriver instance, we have to create an Options object that allows us to specify how exactly we we want to launch Chrome. Let’s tell it that we want the browser to launch headless and that the window size should be set to 1920x1080. We also need ChromeDriver to be able to run Chrome at all — download it here.

Launching Chrome

Now that we have everything we need, we can jump into action! Create an instance of Chrome webdriver and pass it the the Options object we created earlier and the path to the actual ChromeDriver tool. Using the driver, go to the Google homepage, find the I’m Feeling Lucky button and click it. Lastly, take a screenshot!

Results

You should now have a screenshot of the I’m Feeling Lucky page sitting neatly in your project folder! Head over to my github repo for the source code in full.

--

--