• Skip to primary navigation
  • Skip to main content

data Rebellion

Learning through Adventure

  • Home
  • Blog
  • Beginner Python Course
    • Preface – The Journey Ahead
    • Prologue – The Door
    • Chapter 1 – Arithmetic and Variables
    • Chapter 2 – Strings and Lists
    • Chapter 3 – Conditional Statements
    • Chapter 4 – Functions
    • Chapter 5 – Loops
    • Chapter 6 – Built-in Functions and Methods
    • Chapter 7 – Imports and Nesting
    • Chapter 8 – Opening the Door
    • Epilogue – Only the Beginning
  • About
  • Contact
You are here: Home / Selenium / Using Headless Chrome with Selenium in Python

Using Headless Chrome with Selenium in Python

Updated March 25, 2021. Published March 14, 2018. 12 Comments

Headless Chrome and regular Chrome have the same capabilities, and running them with Selenium is a very similar process. The difference is that Headless Chrome does not generate any sort of user interface. In other words, no browser is visibly launched.

If you happen to be web scraping with Selenium, it’s often helpful to see what exactly the browser is doing in real time for development and debugging purposes. However, using headless mode can be great if your script is working and you don’t want to be bothered with an open browser. And even better, a headless browser should generally run faster than its headed counterpart, given that it doesn’t require the extra resources normally needed to visually render everything happening in the browser.

Automating the Chrome browser with Python and Selenium

To run Headless Chrome, you’ll first need to set up Selenium.

Once you’ve got Selenium working, using Headless Chrome is a breeze. For example, let’s see if we can get to DuckDuckGo‘s home page.

Since Headless Chrome has no visible browser, we’ll take a screenshot to confirm what the browser is doing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from selenium import webdriver
 
chromedriver = 'C:\\Users\\grayson\\Downloads\\chromedriver.exe'
 
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1200x600') # optional
 
browser = webdriver.Chrome(executable_path=chromedriver, chrome_options=options)
 
browser.get('https://www.duckduckgo.com')
 
browser.save_screenshot('C:\\Users\\grayson\\Downloads\\headless_chrome_test.png')
 
browser.quit()

If the script finishes and your screenshot shows DuckDuckGo’s home page, you’re all set!

If something’s not working, make sure Selenium, Chrome, and chrome driver are all up-to-date and try again. If all else fails, leave a comment and we’ll figure it out.

Python Automation Project Ideas Ebook Front Page

Free Ebook: 88 Python Project Ideas for Automating Your Life

The best way to learn programming is by working on real-world projects, so why not work on projects that also save you time and sanity? In this free, curated collection, you'll find project ideas for automating:

  • Common office tasks
  • Birthday gifts and wishes
  • Grocery and meal planning
  • Relationships (just the tedious parts!)
  • And quite a bit more

Subscribe to Data Rebellion and get this Ebook delivered straight to your inbox, as well as other exclusive content from time to time on efficiently learning to code useful things, vanquishing soul-crushing work, and having fun along the way.

Reader Interactions

Comments

  1. Aadil says

    May 25, 2018 at 9:14 am

    Hi, how would I add a username and password if authentication is required?
    I am trying to access an Intranet page, and a box pops up asking me for a username and password.

    So currently I get a 401 error code response.
    Thank you

    Reply
    • Grayson Stanton says

      June 5, 2018 at 7:11 pm

      Hi Aadil. First you’ll need to locate the username and password input elements with xpath or css selectors. It’ll probably look something like username_element = browser.find_element_by_xpath('//input[@name="username"]')

      Then you’ll need to send the login credentials to their respective elements with the send_keys() method, which will probably look something like username_element.send_keys("Aadil")

      Finally, you’ll need to either simulate pressing the Enter key or go through a similar process as above to locate and click the login button using the click() method.

      Let me know if this doesn’t work for you.

      Reply
  2. Aadil says

    June 6, 2018 at 4:46 am

    Hi, thanks for the response. Sorry – I wasn’t too clear in my question.
    This is for proxy authentication, so not really credentials on the page but rather a popup from the browser.

    Reply
    • Grayson Stanton says

      June 6, 2018 at 11:23 pm

      Oh I see, so you need to switch to a different window. In that case, you’ll probably want to use the browser.switch_to_window() method and browser.window_handles. Check out the links below for more details:
      https://selenium-python.readthedocs.io/navigating.html
      https://stackoverflow.com/questions/10629815/handle-multiple-window-in-python
      https://stackoverflow.com/questions/8631500/click-the-javascript-popup-through-webdriver
      https://stackoverflow.com/questions/17676036/python-webdriver-to-handle-pop-up-browser-windows-which-is-not-an-alert

      Reply
  3. Vivek says

    October 4, 2018 at 5:24 am

    My browser and Driver is upto date. Chrome.
    But i get a blank screen on taking screenshots, any idea why?

    Reply
    • Grayson Stanton says

      October 5, 2018 at 2:05 am

      Hi Vivek. I’m not sure why that would be. Does everything work fine, and do you still get a blank screenshot, when you aren’t running it in headless mode?

      The other things you might check are that your version of Selenium is up-to-date, and that you’re using a somewhat recent version of Python 3.

      Reply
  4. Anonymous says

    June 13, 2019 at 7:41 pm

    Thanks a lot. Content is really helpful.

    Reply
    • Grayson Stanton says

      June 14, 2019 at 12:17 am

      Sure thing, glad to hear it.

      Reply
  5. Armine Papikyan says

    May 5, 2020 at 11:09 am

    hi. I do all of that staff and everything is up-to-date however I can still see the chrome popping up. Any idea why that could happen?

    Reply
  6. SiriusBlack007 says

    June 7, 2020 at 7:47 pm

    so it worked thanks a lot.
    but does it open a window and then quit it or i have problem with the ‘headless’ part ?
    idk what should headless do exactly

    Reply
  7. SiriusBlack007 says

    June 7, 2020 at 7:50 pm

    oh never mind i forgot the ‘chrome_options=Options’ part
    thanks a lot fro your effort

    Reply
  8. Anonymous says

    September 17, 2020 at 5:26 pm

    i am still seeing the chrome even applying the same code logic

    Reply

Leave a comment Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copyright © 2023

Terms and Conditions - Privacy Policy