• 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 Firefox with Selenium in Python

Using Headless Firefox with Selenium in Python

Updated April 17, 2021. Published March 15, 2018. 8 Comments

Headless Firefox and regular Firefox have the same capabilities, and running them with Selenium is a very similar process. The difference is that Headless Firefox 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 Firefox browser with Python and Selenium

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

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

Since Headless Firefox 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
 
geckodriver = r'C:\Users\grayson\Downloads\geckodriver.exe'
# note that in linux, file path would be more like geckodriver = '/home/grayson/Downloads/geckodriver'
 
options = webdriver.FirefoxOptions()
options.add_argument('-headless')
 
browser = webdriver.Firefox(executable_path=geckodriver, options=options)
 
browser.get('https://www.duckduckgo.com')
 
browser.save_screenshot(r'C:\Users\grayson\Downloads\headless_firefox_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, Firefox, and gecko 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. Luis says

    January 17, 2019 at 8:59 pm

    This was exactly what I was looking for, thank you so much for this. Now I just need to make my web scraper run on “headless mode” and find a way to host it somewhere so it is continuously working.

    Reply
    • Grayson Stanton says

      January 17, 2019 at 9:33 pm

      Hey thanks Luis, glad it was helpful. For hosting you might give Python Anywhere a look, I’ve used them in the past and had a good experience.

      Reply
  2. letmetry07 says

    July 4, 2019 at 6:38 am

    what about when it says executable may have wrong permissions

    Reply
    • Grayson Stanton says

      July 6, 2019 at 5:50 pm

      First make sure you have up-to-date versions of the chrome/gecko driver and the corresponding browser. Then make sure you are specifying the full path of the driver executable, and not just the folder or folder path, and that you have permission to access the folder in which the driver is stored.

      Reply
  3. Anonymous says

    September 16, 2019 at 10:31 am

    how to run headless firefox?

    Reply
    • Grayson Stanton says

      September 16, 2019 at 5:55 pm

      Have you tried running the code above?

      Reply
  4. Anonymous says

    March 31, 2021 at 8:11 am

    It ain’t working…I tried the code above but it Opens Mozilla and executes the latter part.
    Pls Help Me!!
    I’m on Kali Linux amd64 2020.4
    And using python3

    Reply
    • Grayson Stanton says

      April 17, 2021 at 10:55 pm

      Hmm, the code works fine on windows. I just updated the code to avoid a deprecation warning related to the webdriver options, but I don’t know why it isn’t working for you. I would encourage you to make sure your firefox browser, gecko driver, and selenium package are all up to date.

      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