How to make Firefox headless programmatically in Selenium with Python?

后端 未结 6 919
面向向阳花
面向向阳花 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:43

    My answer:

    set_headless(headless=True) is deprecated. 
    

    https://seleniumhq.github.io/selenium/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.options.html

    options.headless = True
    

    works for me

    0 讨论(0)
  • 2020-11-22 11:50
    Used below code to set driver type based on need of Headless / Head for both Firefox and chrome:
    
    // Can pass browser type 
    
    if brower.lower() == 'chrome':
        driver = webdriver.Chrome('..\drivers\chromedriver')
    elif brower.lower() == 'headless chrome':
        ch_Options = Options()
        ch_Options.add_argument('--headless')
        ch_Options.add_argument("--disable-gpu")
        driver = webdriver.Chrome('..\drivers\chromedriver',options=ch_Options)
    elif brower.lower() == 'firefox':
        driver = webdriver.Firefox(executable_path=r'..\drivers\geckodriver.exe')
    elif brower.lower() == 'headless firefox':
        ff_option = FFOption()
        ff_option.add_argument('--headless')
        ff_option.add_argument("--disable-gpu")
        driver = webdriver.Firefox(executable_path=r'..\drivers\geckodriver.exe', options=ff_option)
    elif brower.lower() == 'ie':
        driver = webdriver.Ie('..\drivers\IEDriverServer')
    else:
        raise Exception('Invalid Browser Type')
    
    0 讨论(0)
  • 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?

    0 讨论(0)
  • 2020-11-22 11:54

    Just a note for people who may have found this later (and want java way of achieving this); FirefoxOptions is also capable of enabling the headless mode:

    FirefoxOptions firefoxOptions = new FirefoxOptions();
    firefoxOptions.setHeadless(true);
    
    0 讨论(0)
  • 2020-11-22 12:07

    The first answer does't work anymore.

    This worked for me:

    from selenium.webdriver.firefox.options import Options as FirefoxOptions
    from selenium import webdriver
    
    options = FirefoxOptions()
    options.add_argument("--headless")
    driver = webdriver.Firefox(options=options)
    driver.get("http://google.com")
    
    0 讨论(0)
  • 2020-11-22 12:09

    To the OP or anyone currently interested, here's the section of code that's worked for me with firefox currently:

    opt = webdriver.FirefoxOptions()
    opt.add_argument('-headless')
    ffox_driver = webdriver.Firefox(executable_path='\path\to\geckodriver', options=opt)
    
    0 讨论(0)
提交回复
热议问题