Selenium
Python
Firefox
AWS
Automation

unable to call firefox from selenium in python on AWS machine

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When Selenium cannot launch Firefox on an AWS machine, the root cause is usually environment-related rather than Selenium syntax. EC2 instances often lack a desktop session, may not have Firefox installed, and may not have a driver available on the path, so the fix starts with the host rather than the Python script.

Prefer Headless Firefox on Server Machines

Most AWS Linux instances do not run a graphical session, so trying to start a normal browser window often fails with display errors. Headless mode avoids that requirement and is the default choice for browser automation on EC2.

python
1from selenium import webdriver
2from selenium.webdriver.firefox.options import Options
3
4options = Options()
5options.add_argument("--headless")
6
7driver = webdriver.Firefox(options=options)
8driver.get("https://example.com")
9print(driver.title)
10driver.quit()

If you only need page loading, form submission, screenshots, or scraping, headless mode is usually all you need. A virtual display such as Xvfb is only necessary when you must run a headed browser session.

Verify That Firefox Exists on the Machine

Selenium cannot start Firefox if the binary is missing. On a fresh instance, confirm the binary is present and callable by the same user who runs the test.

bash
which firefox
firefox --version
python -m pip show selenium

If Firefox is installed in a nonstandard location, point Selenium at it explicitly.

python
1from selenium import webdriver
2from selenium.webdriver.firefox.options import Options
3
4options = Options()
5options.binary_location = "/usr/bin/firefox"
6options.add_argument("--headless")
7
8driver = webdriver.Firefox(options=options)
9driver.quit()

That removes guesswork when the package manager installed Firefox somewhere Selenium does not probe by default.

Handle the Driver Layer Deliberately

Recent Selenium releases can use Selenium Manager to discover or download the driver automatically. When that works, you do not need to manage geckodriver yourself. If the instance has restricted outbound access or you want a pinned driver version, install geckodriver manually and pass its path through a service object.

python
1from selenium import webdriver
2from selenium.webdriver.firefox.options import Options
3from selenium.webdriver.firefox.service import Service
4
5options = Options()
6options.add_argument("--headless")
7service = Service("/usr/local/bin/geckodriver")
8
9driver = webdriver.Firefox(service=service, options=options)
10driver.get("https://example.com")
11driver.quit()

This is often the most predictable setup in CI or hardened server environments.

Check System Dependencies and Permissions

A driver can be present and Firefox can still fail to start if required shared libraries are missing or if the automation user cannot execute the binary. Server images are often minimal, so browser packages may pull in less than you expect.

Also pay attention to which user starts the process. A browser launched successfully as one user may fail under systemd, cron, or another service account because the path, home directory, or permissions differ.

Common Pitfalls

  • Trying to launch a headed Firefox session on an EC2 instance that has no display server.
  • Installing Selenium but not verifying that the firefox binary itself exists and is executable.
  • Assuming driver discovery will work on a machine that cannot reach the network to download or verify a driver.
  • Running the script manually as one user and then wondering why the same code fails under a different service account.
  • Debugging AWS security groups when the problem is actually local browser startup on the instance.

Summary

  • Use headless Firefox by default on AWS servers.
  • Confirm that Firefox is installed and on the expected path.
  • Let Selenium Manager handle the driver when possible, or pass an explicit geckodriver path when you need control.
  • Check user permissions and missing system libraries on minimal images.
  • Treat browser startup issues as host-configuration problems first, and Selenium API problems second.

Course illustration
Course illustration

All Rights Reserved.