Back

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

Is it possible to make selenium use the TOR browser? Does anyone have any code they could copy-paste?

1 Answer

0 votes
by (62.9k points)

Yes, it's possible to make selenium use the TOR browser.

I was able to do this on each Ubuntu and macintosh OS X.

Two things have to happen:

  1. Place the binary location(path) to the firefox binary that Tor uses.

On a macintosh this path would generally be /Applications/TorBrowser.app/Contents/MacOS/firefox. On my Ubuntu machine it is /usr/bin/tor-browser/Browser/firefox.

       2. The Tor browser uses a SOCKS host at 127.0.0.1:9150 either through Vidalia or Tor installation.

Launch Tor once from the Finder and leave it open in order that Vidalia is going to be running.

The instances launched with selenium can use the SOCKS host that Vidalia starts, too.

Here is the code to accomplish those two things. I run this on Mac OS X Yosemite:

import os

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

from selenium import webdriver

# path to the firefox binary inside the Tor package

binary = '/Applications/TorBrowser.app/Contents/MacOS/firefox'

if os.path.exists(binary) is False:

raise ValueError("Binary path to Tor firefox is invalid.")

firefox_binary = FirefoxBinary(binary)

browser = None

def get_browser(binary=None):

global browser

# just one instance of a browser opens, remove global for multiple instances

if not browser:

browser = webdriver.Firefox(firefox_binary=binary)

return browser

if __name__ == "__main__":

browser = get_browser(binary=firefox_binary)

urls = (

('tor browser check', 'https://check.torproject.org/'),

('ip checker', 'http://icanhazip.com')

)

for url_name, url in urls:

print "getting", url_name, "at", url

browser.get(url)

Using an Ubuntu operating system I ran the Tor browser via selenium.

This machine has tor running at port 9051 and Privoxy hypertext transfer protocol proxy that uses tor at port 8118.

In order for the Tor browser to pass the tor check page, I had to set the HTTP proxy to privoxy.

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

from selenium.webdriver.common.proxy import Proxy, ProxyType

from selenium import webdriver

browser = None

proxy_address = "127.0.0.1:8118"

proxy = Proxy()

tor = '/usr/bin/tor-browser/Browser/firefox'

firefox_binary = FirefoxBinary(tor)

urls = (

('tor_browser_check', 'https://check.torproject.org/'),

('icanhazip', 'http://icanhazip.com'),

)

keys, _ = zip(*urls)

urls_map = dict(urls)

def get_browser(binary=None, proxy=None):

global browser

if not browser:

browser = webdriver.Firefox(firefox_binary=binary, proxy=proxy)

return browser

if __name__ == "__main__":

browser = get_browser(binary=firefox_binary, proxy=proxy)

for resource in keys:

browser.get(urls_map.get(resource))

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training!

by (100 points)
for windows, how can i manage it? regards

Browse Categories

...