Python - Firefox Headless

后端 未结 5 582
孤独总比滥情好
孤独总比滥情好 2020-12-05 05:38

I\'ve spent the last few days messing around with Selenium, Tor, and Firefox as a combination for multiple tasks. I\'ve managed to write a simple script in Python that takes

相关标签:
5条回答
  • 2020-12-05 05:55

    Or alternatively use a true headless browser, like Phantomjs which is light weighted and nicely integrated with selenium

    from selenium import webdriver
    driver=webdriver.PhantomJS('your pahtomjs exe file locaiton')
    
    0 讨论(0)
  • 2020-12-05 06:07

    if finally find the answer:

    First, of first do these:
    Take care that you set fire fox drive path correctly.

    And then:

    sudo apt-add-repository ppa:mozillateam/firefox-next
    sudo apt-get update
    sudo apt-get install firefox xvfb
    Xvfb :10 -ac &
    export DISPLAY=:10

    And at the end run this command to see that do we have any error in our implementation of not.

    firefox

    and if there no any output just click ctrl+c.
    Ok, after that write this codes.

    from selenium import webdriver
    
    class FireFoxLoadTest:
        def __init__(self):
            # 1 - Load a fire fox web driver
            self.driver = webdriver.Firefox()
    
        def do_test(self, url):
            # 2 - Start to check url on the fire fox browser
            result = self.driver.get(url)
            self.driver.quit()
            return self.result
    
    fire_fox = FireFoxLoadTest()
    res = fire_fox.do_test('http://www.google.com')
    print(res)
    
    0 讨论(0)
  • 2020-12-05 06:10

    xvfb is a common way of doing this. Searching for "selenium xvfb" should find lots, such as:

    • Is it possible to run Selenium scripts without having an X server running, too?
    • How do I run Selenium in Xvfb?
    • http://www.alittlemadness.com/2008/03/05/running-selenium-headless/
    0 讨论(0)
  • 2020-12-05 06:11

    Since version 56 release at September 28, 2017, Firefox headless mode is available in all three main operating system.

    You can set headless mode through webdriver.FirefoxOptions(), just like you did with Chrome:

    from selenium import webdriver
    
    options = webdriver.FirefoxOptions()
    options.add_argument('headless')
    driver = webdriver.Firefox(options=options)
    

    P.S. If you use Selenium < 3.8.0, you have to replace webdriver.FirefoxOptions() with webdriver.firefox.options.Options() (see PR #5120).

    Besides, use enviroment variable MOZ_HEADLESS will do the same thing:

    import os
    from selenium import webdriver
    
    os.environ['MOZ_HEADLESS'] = '1'  # <- this line
    driver = webdriver.Firefox()
    
    0 讨论(0)
  • 2020-12-05 06:14

    There is progress being made on headless firefox.

    From April 21st, 2017, https://adriftwith.me/coding/2017/04/21/headless-slimerjs-with-firefox/

    tl;dr Firefox Nightly on Linux supports running SlimerJS headlessly.
    More platforms and full headless Firefox are coming soon.

    0 讨论(0)
提交回复
热议问题