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.
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.
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()
browser = webdriver.Firefox(executable_path=geckodriver, options=options)
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.