How to make Firefox headless programmatically in Selenium with Python?

后端 未结 6 1003
面向向阳花
面向向阳花 2020-11-22 11:37

I am running this code with python, selenium, and firefox but still get \'head\' version of firefox:

binary = FirefoxBinary(\'C:\\\\Program Files (x86)\\\\Mo         


        
6条回答
  •  礼貌的吻别
    2020-11-22 11:51

    To invoke Firefox Browser headlessly, you can set the headless property through Options() class as follows:

    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')
    driver.get("http://google.com/")
    print ("Headless Firefox Initialized")
    driver.quit()
    

    There's another way to accomplish headless mode. If you need to disable or enable the headless mode in Firefox, without changing the code, you can set the environment variable MOZ_HEADLESS to whatever if you want Firefox to run headless, or don't set it at all.

    This is very useful when you are using for example continuous integration and you want to run the functional tests in the server but still be able to run the tests in normal mode in your PC.

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

    or

    $ export MOZ_HEADLESS=1   # this way you only have to set it once
    $ python manage.py test functional/tests/directory
    $ unset MOZ_HEADLESS      # if you want to disable headless mode
    

    Outro

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

提交回复
热议问题