Using Selenium WebDriver with Tor

Because Tor Browser Bundle is just a patched version of Firefox, it seems that it should be possible to use a FirefoxDriver with Tor Browser. This is what I've tried so far:

String torPath = "C:\\Users\\My User\\Desktop\\Tor Browser\\Start Tor Browser.exe";
String profilePath = "C:\\Users\\My User\\Desktop\\Tor Browser\\Data\\Browser\\profile.default";
FirefoxProfile profile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxDriver driver = new FirefoxDriver(binary, profile);
driver.get("http://www.google.com");

This results in a blank Tor Browser page opening with a popup message: Your Firefox profile cannot be loaded. It may be missing or inaccessible.

I know that the profile is valid/compatible because I can successfully start the browser and profile with:

binary.startProfile(profile, profilePath, ""));

I don't know how to send commands to a browser opened in such a manner, however.

I've found similar questions, but I'm specifically looking for a Java solution, preferably tested on Windows.

I'm using a standalone Selenium library that can be downloaded here and the Tor Browser Bundle that can be downloaded here.

Answers


Because Tor Browser Bundle wasn't letting me use the WebDriver extension, I found a workaround in which I ran Tor from a regular Firefox browser. With this method, as long as the Tor Browser is open, you can use Tor with a regular Firefox browser.

  • Open Tor Browser:

    File torProfileDir = new File(
            "...\\Tor Browser\\Data\\Browser\\profile.default");
    FirefoxBinary binary = new FirefoxBinary(new File(
            "...\\Tor Browser\\Start Tor Browser.exe"));
    FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
    torProfile.setPreference("webdriver.load.strategy", "unstable");
    
    try {
        binary.startProfile(torProfile, torProfileDir, "");
    } catch (IOException e) {
        e.printStackTrace();
    }
    
  • Open Firefox with some configurations:

    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("network.proxy.type", 1);
    profile.setPreference("network.proxy.socks", "127.0.0.1");
    profile.setPreference("network.proxy.socks_port", 9150);
    FirefoxDriver = new FirefoxDriver(profile);
    
  • Close browsers. Note that if you plan on doing a lot of closing and reopening (useful in obtaining a new IP address), I advise setting the profile preference toolkit.startup.max_resumed_crashes to a high value like 9999.

    private void killFirefox() {
        Runtime rt = Runtime.getRuntime();
    
        try {
            rt.exec("taskkill /F /IM firefox.exe");
            while (processIsRunning("firefox.exe")) {
                Thread.sleep(100);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private boolean processIsRunning(String process) {
        boolean processIsRunning = false;
        String line;
        try {
            Process proc = Runtime.getRuntime().exec("wmic.exe");
            BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            OutputStreamWriter oStream = new OutputStreamWriter(proc.getOutputStream());
            oStream.write("process where name='" + process + "'");
            oStream.flush();
            oStream.close();
            while ((line = input.readLine()) != null) {
                if (line.toLowerCase().contains("caption")) {
                    processIsRunning = true;
                    break;
                }
            }
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return processIsRunning;
    }
    

I would try specifying the path of one of the existing profiles and initialize your profile instance for Tor so your code would look something like:

String torPath = "..\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "..\\Tor Browser\\Data\Browser\\profile.default";
FirefoxProfile profile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxDriver driver = new FirefoxDriver(binary, profile);
driver.get("http://www.google.com");

I didn't try this since I don't have WebDriver setup at home, but this should allow you to specify a profile.


I downloaded TorBrowser and I was able to call it without any problem, with the following code (it's Mac OS). So I think there shouldn't be any problem about compatibility between the firefox webdriver and the latest version of Tor Browser.

String torPath = "/Volumes/DATA/Downloads/Tor.app/Contents/MacOS/TorBrowser.app/Contents/MacOS/firefox";
String profilePath = "/Users/mimitantono/Library/Application Support/Firefox/Profiles/1vps9kas.default-1384778906995";
FirefoxProfile profile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxDriver driver = new FirefoxDriver(binary, profile);
driver.get("http://www.google.com/webhp?complete=1&hl=en");

I know that you already tested the path of your profile with binary.startProfile but I think you could try again to use slash instead of backslash to specify the path, cross check whether that file exists -> profile.default, and to use absolute path instead of relative -> ../.


This code also is working pretty good in ubuntu. Here is an example (JUnit4):

package qa2all;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class HTMLUnit {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        //driver = new HtmlUnitDriver();    
        //driver = new FirefoxDriver();
        String torPath = "/home/user/Dropbox/Data/TorBrowser/Linux/32/start-tor-browser";
        String profilePath = "/home/user/Dropbox/Data/TorBrowser/Linux/32/TorBrowser/Data/Browser/profile.default/";
        FirefoxProfile profile = new FirefoxProfile(new File(profilePath));
        FirefoxBinary binary = new FirefoxBinary(new File(torPath));
        driver = new FirefoxDriver(binary, profile);        
        baseUrl = "https://qa2all.wordpress.com";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testUntitled() throws Exception {
        driver.get(baseUrl + "/");

    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private void fail(String verificationErrorString) {
        // TODO Auto-generated method stub

    }
}

If you mostly care about remote-controlling a browser using Tor (and would maybe prefer to use the Tor Browser instead of vanilla Firefox), you can use Mozilla's Marionette. Use like

To use with the Tor Browser, enable marionette at startup via

Browser/firefox -marionette

(inside the bundle). Then, you can connect via

from marionette import Marionette
client = Marionette('localhost', port=2828);
client.start_session()

and load a new page for example via

url='http://mozilla.org'
client.navigate(url);

For more examples, there is a tutorial along with more documentation.

(copy)


Need Your Help

Is there ever a time where using a database 1:1 relationship makes sense?

sql database-design one-to-one database-normalization

I was thinking the other day on normalization, and it occurred to me, I cannot think of a time where there should be a 1:1 relationship in a database.

Determine number of pages in a PDF file

c# pdf .net-2.0

I need to determine the number of pages in a specified PDF file using C# code (.NET 2.0). The PDF file will be read from the file system, and not from a URL. Does anyone have any pointers on how ...