因为Tor浏览器捆绑包不允许我使用WebDriver扩展,所以我找到了一种解决方法,可以从常规的Firefox浏览器中运行Tor。使用此方法,只要打开Tor浏览器,就可以将Tor与常规Firefox浏览器一起使用。
- 打开Tor浏览器 :
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(); }
- *使用某些配置 *打开Firefox :
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);
- 关闭浏览器 。请注意,如果您打算进行很多关闭和重新打开 *** 作(有助于获取新的IP地址),建议将配置文件首选项设置为
toolkit.startup.max_resumed_crashes
,例如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; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)