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.
- Use Command Line (Only simple command like print to PDF, take screenshot, etc.
- Use Selenium
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.
- 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.
- Personally, I think PhantomJS and SlimmerJS are better choices for now.