A headless browser is a web browser without a user interface, it means the browser is running in the background (invisbile). This is great if you want to start a web browser to do tasks, but you don’t want or need to see it.

You can use any Web Browser like Firefox or Chrome in a headless mode. To do so, first open up the Web Browser using the Web Driver and then set it to headless mode. In any case, the web driver needs to be installed.

Related course:

selenium

selenium firefox headless

Before you start, make sure the Web Browser, the Web Driver and the selenium module are all installed and working.

The code below starts Firefox, but in headless mode. It can do anything Firefox can, but it wont be visible on the screen. The screenshot below outputs the html code of the web page, but that’s optional.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from selenium import webdriver

try:
fireFoxOptions = webdriver.FirefoxOptions()
fireFoxOptions.set_headless()
brower = webdriver.Firefox(firefox_options=fireFoxOptions)

brower.get('https://pythonbasics.org')
print(brower.page_source)
finally:
try:
brower.close()
except:
pass

selenium firefox headless

It’s set as headless browser here, where FirefoxOptions() is used to make it headless.

1
2
3
fireFoxOptions = webdriver.FirefoxOptions()
fireFoxOptions.set_headless()
brower = webdriver.Firefox(firefox_options=fireFoxOptions)

After loading the headless web browser, you can proceed to using it as you would normally do with selenium. The only difference is that it’s not visible to the user.

If you are new to selenium, then I highly recommend this book.

Download examples