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:
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!