Home >Backend Development >Python Tutorial >How to Run Selenium Tests with Firefox Headless?

How to Run Selenium Tests with Firefox Headless?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-16 10:26:03724browse

How to Run Selenium Tests with Firefox Headless?

Headless Firefox with Python and Selenium

Running Selenium tests with Firefox headless can be crucial for automated testing in headless environments. However, encountering the "head" version of Firefox can pose challenges.

Solution

To resolve this issue and invoke Firefox headless, utilize the Options() class.

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

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'path/to/geckodriver.exe')
driver.get("http://google.com/")
print("Headless Firefox Initialized")
driver.quit()

Alternatively, set the environment variable MOZ_HEADLESS to enable/disable headless mode without modifying code:

$ MOZ_HEADLESS=1 python manage.py test # testing example in Django with headless Firefox

Additional Options

Visit the following resources for further insights:

  • YouTube Videos on Mozilla Firefox and Headless Chrome with Selenium
  • Configuring ChromeDriver for headless Chrome in Selenium

These advanced options provide comprehensive solutions for managing Firefox and other browsers in headless mode.

The above is the detailed content of How to Run Selenium Tests with Firefox Headless?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn