使用Selenium WebDriver和Tor

因为Tor Browser Bundle只是Firefox的补丁版本,所以似乎可以使用带有Tor浏览器的FirefoxDriver 。 这是我到目前为止所尝试的:

 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"); 

这会导致一个空白的Tor浏览器页面打开,并显示一条弹出消息: 无法加载您的Firefox配置文件。 它可能丢失或无法访问。

我知道该配置文件是有效/兼容的,因为我可以成功启动浏览器和配置文件:

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

但是,我不知道如何将命令发送到以这种方式打开的浏览器。

我发现了类似的问题,但我特意寻找Java解决方案,最好是在Windows上测试。

我正在使用可在此处下载的独立Selenium库和可在此处下载的Tor Browser Bundle。

因为Tor Browser Bundle不允许我使用WebDriver扩展,所以我找到了一种解决方法,我从常规的Firefox浏览器中运行Tor。 使用此方法,只要Tor浏览器打开,您就可以使用常规Firefox浏览器使用Tor。

  • 打开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; } 

我会尝试指定一个现有配置文件的路径并初始化Tor的配置文件实例,因此您的代码看起来像:

 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"); 

我没有尝试这个,因为我家里没有WebDriver设置,但这应该允许你指定一个配置文件。

我下载了TorBrowser,我可以毫无问题地调用它,使用以下代码(它是Mac OS)。 所以我认为firefox webdriver和最新版本的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"); 

我知道您已经使用binary.startProfile测试了配置文件的路径,但我认为您可以再次尝试使用斜杠而不是反斜杠来指定路径,交叉检查该文件是否存在 – > profile.default ,并使用绝对路径而不是相对 – > ../

这段代码在ubuntu中也运行得很好。 这是一个例子(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 } } 

如果您最关心使用Tor远程控制浏览器(并且可能更喜欢使用Tor浏览器而不是使用Torilla Firefox),则可以使用Mozilla的Marionette 。 使用像

要与Tor浏览器一起使用,请在启动时启用marionette

 Browser/firefox -marionette 

(在捆绑内)。 然后,您可以通过连接

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

并加载一个新页面,例如通过

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

有关更多示例,请参阅教程以及更多文档。

( 副本 )