Selenium with Chrome Headless

Google Chrome has released a new awesome feature, “HEADLESS”. It was delivered with Chrome 59 on MacOS and Chrome 60 on Windows.

Headless browser is an invisible browser. It doesn’t have GUI.
So you have a couple choices to interact with the Chrome headless.

In this article, I will talk about Selenium only.

In order to launch a Chrome in headless mode, we just need to add an option ‘–headless’ when we create a new instance of WebDriver. You may need to upgrade chromedriver and Selenium.
See an example in Python below.

from selenium import webdriverfrom selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
future.driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get(u'http://stackoverflow.com')
questions = driver.find_elements_by_css_selector('#question-mini-list .question-summary .summary h3')
for question in questions: 
    print(question.text)

driver.quit()

Here is the result.

 

NOTE

  1. Currently, the headless mode still consumes similar memory as Chrome. So if you need it because of lacking of resources, headless mode is not a good choice for now (Aug 2017). But I’m sure the development team will try to improve that.
  2. Personally, I think PhantomJS and SlimmerJS are better choices for now.

Leave a Reply

Your email address will not be published. Required fields are marked *