Using Selenium WebDriver with Tor
Solution 1:
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 like9999
.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; }
Solution 2:
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.
Solution 3:
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 -> ../
.
Solution 4:
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
}
}