使用Selenium WebDriver和Tor

Joe*_*hel 22 java firefox tor selenium-webdriver tor-browser-bundle

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

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");
Run Code Online (Sandbox Code Playgroud)

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

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

binary.startProfile(profile, profilePath, ""));
Run Code Online (Sandbox Code Playgroud)

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

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

我使用的是独立硒库,可以下载在这里和Tor浏览器套件,可以下载在这里.

Joe*_*hel 19

因为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();
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用一些配置打开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);
    
    Run Code Online (Sandbox Code Playgroud)
  • 关闭浏览器.请注意,如果您计划进行大量关闭和重新打开(在获取新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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • 我试图按照此步骤执行,但是在执行 TOR 时出现错误,当浏览器启动时,会弹出一个说 **TOR 无法启动**。 (3认同)