Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in DevOps and Agile by (19.7k points)

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 into 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!

1 Answer

0 votes
by (62.9k points)

Since version 56 release on September 28, 2017, Firefox headless mode is out there in all 3 main OS.

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

from selenium import webdriver

options = webdriver.FirefoxOptions()

options.add_argument('headless')

driver = webdriver.Firefox(options=options)

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

Besides, use environment variable MOZ_HEADLESS can do the same thing:

import os

from selenium import webdriver

os.environ['MOZ_HEADLESS'] = '1'  # <- this line

driver = webdriver.Firefox()

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...