Firefox can be controlled by Python. To do this you need the selenium module and a web driver. The Python code starts the web browser and then completely controls it.

The code can then do anything you can do with a web browser, like opening a page, sending key presses or button clicks.

Related course:

Firefox

Selenium Firefox Example

To make Firefox work with Python selenium, you need to install the geckodriver. The geckodriver driver will start the real firefox browser and supports Javascript.
From python you can load the Firefox browser with one line of code:

1
from selenium import webdriver

Take a look at the selenium firefox code. First import the webdriver, then make it start firefox.
Open a webage with the get page and optionally send keypresses.

1
2
3
4
5
6
7
8
# coding=utf-8
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://dev.to")

driver.find_element_by_id("nav-search").send_keys("Selenium")

selenium firefox

What is GeckoDriver?

The web browser Mozilla Firefox uses an engine named the Gecko browser engine. The engine was created by the Mozilla foundation.

Because it’s an engine, it can be used in other web browsers (just like how engines can be used in other cars). Every browser has their own engine, but sometimes they use the same engine to display web pages.

GeckoDriver is what is between Selenium and the FireFox browser. It lets you control the Firefox web browser from Python code. All web browser commands go through the GeckoDriver, the GeckoDriver in turn makes your browser do what you want.

The GeckoDriver is a different executable on every operating system. On Windows it is GeckoDriver.exe, but on Mac there are no .exe files, so it’s named differently.

The GeckoDriver must match the Firefox version, otherwise you can get incompatibility issues or have the issue that it simply doesn’t work.

Headless Firefox

There are several parameters you can specify, one of them is headless. If you want to make Firefox headless (invisible), you add that as parameter in FirefoxOptions.

1
2
3
4
5
6
from selenium.webdriver.firefox.options import Options as FirefoxOptions

options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
driver.get("https://pythonbasics.org")

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

Download examples