Home >Backend Development >Python Tutorial >How to Run Firefox in Headless Mode with Selenium and Python?

How to Run Firefox in Headless Mode with Selenium and Python?

Barbara Streisand
Barbara StreisandOriginal
2024-11-25 19:50:11582browse

How to Run Firefox in Headless Mode with Selenium and Python?

Troubleshooting Firefox Headless Mode in Selenium with Python

Despite using Selenium and FirefoxBinary with command-line arguments, you may still encounter Firefox running in its "head" version. To resolve this issue and successfully invoke Firefox headless, follow these steps:

Using Selenium Options() Class

In your Python script, you can set the headless property in the Options() class as shown below:

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

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')

Environment Variable

Alternatively, you can set the environment variable MOZ_HEADLESS to any non-zero value to run Firefox headless:

$ MOZ_HEADLESS=1 python manage.py test

To enable/disable headless mode on the fly without modifying code, you can export the variable as follows:

$ export MOZ_HEADLESS=1
$ python manage.py test …
$ unset MOZ_HEADLESS

Additional Resources

  • Video Tutorial: Mozilla Firefox in Headless Mode through Selenium 3.5.2 (Java)
  • Article: Login into Gmail Account using Headless Chrome through Selenium Java

Related Question

How to configure ChromeDriver to initiate Chrome browser in headless mode through Selenium?

The above is the detailed content of How to Run Firefox in Headless Mode with Selenium and Python?. 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