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.
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.
Luis says
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.
Grayson Stanton says
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.
letmetry07 says
what about when it says executable may have wrong permissions
Grayson Stanton says
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.
Anonymous says
how to run headless firefox?
Grayson Stanton says
Have you tried running the code above?
Anonymous says
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
Grayson Stanton says
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.