Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu: Install tor browser & use it with Selenium Python

How can I install the tor browser to make it useable in Python using Selenium?

I have tried sudo apt-get install tor-browser, but I don't know where it gets installed, hence what to put in the PATH variable (or in executable-path).

My goal is to

  1. install Tor browser

  2. open Tor Browser with Python Selenium

  3. go to a website.

like image 857
John Avatar asked Jun 16 '26 04:06

John


1 Answers

I was able to run the Tor Browser on MacOS using Selenium. First, install the Tor Browser by downloading the appropriate package from here. MacOS will prompt you to move the contents to the Applications folder. Run TorBrowser to see if it works. Leave the TorBrowser running and run the following code.

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary("/Applications/TorBrowser.app/Contents/MacOS/firefox")
driver = webdriver.Firefox(firefox_binary= binary)
driver.get("https://check.torproject.org/")

On Ubuntu, you can just leave the unzipped folder where it is. Test that TorBrowser works by double-clicking the TorBrowser icon in the folder. After spending a lot of time trying to make Tor work with Selenium on Ubuntu, I gave up and concluded that it might be easier to use a different tool specialized for Tor. I tried tor-browser-selenium link, which worked like a charm. This the code from that repository that worked for me:

from tbselenium.tbdriver import TorBrowserDriver
with TorBrowserDriver("/path/to/tbb/tor-browser_en-US/") as driver:
    driver.get('https://check.torproject.org') 

Prior to running this above code, you may have to export the environment variable TBB_PATH as follows, otherwise you will get an error stating that TBB_PATH is undefined:

export TBB_PATH=/path/to/tbb/tor-browser_en-US/

I didn't try Windows.

like image 58
amitdatta Avatar answered Jun 18 '26 00:06

amitdatta