How to use tor browser with selenium

  • 7 years ago
Pre-Requirements:
1. Mozilla firefox
2. Eclipse with selenium
3. Tor installed

---------------------------------------------------------------------------------------------------------------------------------------------
Code:

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "C:/Users/Jay/Desktop/Tor Browser/geckodriver.exe"); File torProfileDir = new File( "C:/Users/Jay/Desktop/Tor Browser/Browser/TorBrowser/Data/Browser/profile.default");
FirefoxBinary binary = new FirefoxBinary(new File( "C:/Users/Jay/Desktop/Tor Browser/Browser/firefox.exe"));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
torProfile.setPreference("webdriver.load.strategy", "unstable");

try { binary.startProfile(torProfile, torProfileDir, "");
} catch (IOException e) { e.printStackTrace();
} 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 driver= new FirefoxDriver(profile);
Thread.sleep(10000); driver.get("http://www.google.com/");
Thread.sleep(5000);
killFirefox(); }

private static 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 static 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;
}