Python - Firefox Headless

2019-01-13 21:44发布

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 control of Firefox through Selenium, while Firefox is connected to Tor for privacy.

Now, I'm looking for a way to save resources, so I thought of running Firefox in headless mode, which I thought was a common feature but it doesn't seem to be that. I'm looking for a method to do just that. The reason for it being Firefox and not some terminal based browser is because of the extension "TorButton" that I'm using within Firefox. It has javascript injections built in to it that help with privacy.

If anyone has done this before (which I'm sure many have!), some tips would be greatly appreciated, thank you!

5条回答
成全新的幸福
2楼-- · 2019-01-13 22:09

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()
查看更多
Animai°情兽
3楼-- · 2019-01-13 22:20

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.

查看更多
戒情不戒烟
5楼-- · 2019-01-13 22:24

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')
查看更多
老娘就宠你
6楼-- · 2019-01-13 22:31

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)
查看更多
登录 后发表回答