Python - Firefox Headless

a 夏天 提交于 2019-11-27 19:49:47

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()

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')

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.

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!