0
0
Selenium Pythontesting~5 mins

Firefox configuration in Selenium Python

Choose your learning style9 modes available
Introduction

We configure Firefox in Selenium to control how the browser behaves during tests. This helps us test real user scenarios and handle browser settings easily.

When you want to set a custom homepage before tests start.
When you need to disable browser notifications during testing.
When you want to run Firefox in headless mode to save resources.
When you want to add browser extensions for testing.
When you want to set download folder location automatically.
Syntax
Selenium Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service

options = Options()
# Example: options.headless = True

service = Service(executable_path='path_to_geckodriver')
driver = webdriver.Firefox(service=service, options=options)

Use Options() to set Firefox-specific settings like headless mode.

Use Service() to specify the path to the geckodriver executable.

Examples
This example runs Firefox without opening a window (headless mode).
Selenium Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True

driver = webdriver.Firefox(options=options)
driver.get('https://example.com')
print(driver.title)
driver.quit()
This example disables web notifications in Firefox during the test.
Selenium Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference('dom.webnotifications.enabled', False)

driver = webdriver.Firefox(options=options)
driver.get('https://example.com')
driver.quit()
This example sets Firefox to download PDF files automatically to a specific folder.
Selenium Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference('browser.download.folderList', 2)
options.set_preference('browser.download.dir', '/tmp/downloads')
options.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')

driver = webdriver.Firefox(options=options)
driver.get('https://example.com/sample.pdf')
driver.quit()
Sample Program

This test opens Firefox in headless mode, disables notifications, navigates to example.com, and prints the page title.

Selenium Python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
options.set_preference('dom.webnotifications.enabled', False)

with webdriver.Firefox(options=options) as driver:
    driver.get('https://example.com')
    title = driver.title
    print(f'Page title is: {title}')
OutputSuccess
Important Notes

Always use the latest geckodriver compatible with your Firefox version.

Headless mode is useful for running tests on servers without a display.

Setting preferences helps simulate user settings and avoid pop-ups during tests.

Summary

Firefox configuration in Selenium controls browser behavior during tests.

Use Options() to set preferences like headless mode or disable notifications.

Proper configuration helps tests run smoothly and mimic real user environments.