【问题标题】:Using Selenium WebDriver with Tor将 Selenium WebDriver 与 Tor 一起使用
【发布时间】:2014-04-10 03:23:38
【问题描述】:

因为 Tor Browser Bundle 只是 Firefox 的一个补丁版本,看来应该可以将 FirefoxDriver 与 Tor 浏览器一起使用。这是我迄今为止尝试过的:

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 库here 和可以下载的 Tor 浏览器捆绑包here。

【问题讨论】:

  • 根据Tor browser design page,编译到Firefox版本中的补丁会阻止扩展执行。由于 Firefox 驱动程序的“远程端”是作为浏览器扩展实现的,这是否会影响您使用 WebDriver 驱动 Tor 浏览器的能力?
  • 我也想过这个,但似乎它是由下面的 Mimi 实现的。另外,我已经让它在 Linux 上运行。
  • 您的代码与下面指定的代码之间的一个区别是您正在启动“Start Tor Browser.exe”,而其他代码正在启动嵌入式 Firefox 可执行文件。如果我将您的FirefoxBinary 更改为指向C:\Users\<My User Name>\Desktop\Tor Browser\Browser\firefox.exe,我可以正常启动浏览器,但WebDriver 扩展肯定不会加载。
  • 我试过了,但我得到了相同的结果。

标签: java firefox selenium-webdriver tor tor-browser-bundle


【解决方案1】:

因为 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 时出现错误,当浏览器启动时,会弹出一个提示:TOR failed to start。跨度>
  • @JoseBernhardt 你有完整的代码吗?可能是 GitHub?还是在私人电子邮件中?
  • 警告:您应该几乎永远不要尝试使用 tor 来通过除 tor 浏览器以外的任何方式浏览网页。遵循这个解决方案是一个糟糕的主意,如果你使用它,你应该期望零匿名。是的,tor 浏览器“只是”一个修改过的 firefox。但是,它已经过修改,以防止浏览器泄露您的信息或身份。 Vanilla firefox 不会进行任何此类尝试。
  • @nullUser 听起来像是明智的建议,但您指的是哪些修改?你能给我们扔一根骨头吗?
  • @Robino 阅读torproject.org/projects/torbrowser/design 以及从那里开始的链接。是的,有很多变化。
【解决方案2】:

我会尝试指定现有配置文件之一的路径并为 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,但这应该允许你指定一个配置文件。

【讨论】:

  • 您可能想看看以下问题。 stackoverflow.com/questions/15316304/…
  • 我仍然收到:您的 Firefox 配置文件无法加载。它可能丢失或无法访问。
  • 我会仔细检查配置文件的路径是否确实有效。
  • 只是为了绝对确定,你可以给它绝对路径而不是使用相对路径,看看是否有什么不同?另一种可能性是它无法解析 Tor 实现的配置文件——它们可能使用不同的格式。你有差异工具吗?也许快速浏览一下并将 FF 配置文件目录与 tor 配置文件目录进行比较。我推荐 BeyondCompare,你可以比较单个文件和整个文件夹结构。
  • 在我的示例中,我只提供了路径上对每个人来说都是不变的部分,但实际上我在自己的实现中使用了绝对路径。配置文件目录的内容看起来非常相似,并且 profiles.ini 对两者几乎相同。 (你总是可以试一试,看看会发生什么。我已经提供了所有必要的下载链接。)
【解决方案3】:

我下载了 TorBrowser,我可以毫无问题地调用它,使用以下代码(它是 Mac OS)。 所以我认为firefox webdriver和最新版Tor浏览器的兼容性应该没有问题。

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,然后使用绝对路径而不是相对路径 -> ../.

【讨论】:

  • 我实际上在我的实现中使用了完整的绝对路径。我现在已经展示了绝对路径以避免混淆。至于反斜杠,它们用于 Windows 计算机,所以这绝对不是问题。突出的一件事是您使用了默认的 Firefox 配置文件,而不是 Tor 提供的配置文件。我试过了,但也没有用。
【解决方案4】:

此代码在 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

    }
}

【讨论】:

    【解决方案5】:

    如果您主要关心使用 Tor 远程控制浏览器(并且可能更喜欢使用 Tor 浏览器而不是原版 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);
    

    如需更多示例,请参阅tutorial 以及更多文档。

    (copy)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 2013-04-29
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多