Surprise Me!

How to use tor browser with selenium

2017-03-31 7 Dailymotion

Pre-Requirements: <br />1. Mozilla firefox <br />2. Eclipse with selenium <br />3. Tor installed <br /> <br />--------------------------------------------------------------------------------------------------------------------------------------------- <br />Code: <br /> <br />public static void main(String[] args) throws Exception { <br />// TODO Auto-generated method stub <br />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"); <br />FirefoxBinary binary = new FirefoxBinary(new File( "C:/Users/Jay/Desktop/Tor Browser/Browser/firefox.exe")); <br />FirefoxProfile torProfile = new FirefoxProfile(torProfileDir); <br />torProfile.setPreference("webdriver.load.strategy", "unstable"); <br /> <br />try { binary.startProfile(torProfile, torProfileDir, ""); <br />} catch (IOException e) { e.printStackTrace(); <br />} FirefoxProfile profile = new FirefoxProfile(); <br />profile.setPreference("network.proxy.type", 1); <br />profile.setPreference("network.proxy.socks", "127.0.0.1"); <br />profile.setPreference("network.proxy.socks_port", 9150); <br />FirefoxDriver driver= new FirefoxDriver(profile); <br />Thread.sleep(10000); driver.get("http://www.google.com/"); <br />Thread.sleep(5000); <br />killFirefox(); } <br /> <br />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(); } <br />} <br /> <br />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; <br />}

Buy Now on CodeCanyon