在 Tor 中使用 Selenium WebDriver [英] Using Selenium WebDriver with Tor

查看:24
本文介绍了在 Tor 中使用 Selenium WebDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Because Tor Browser Bundle is just a patched version of Firefox, it seems that it should be possible to use a FirefoxDriver with Tor Browser. This is what I've tried so far:

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 配置文件.它可能丢失或无法访问.

This results in a blank Tor Browser page opening with a popup message: Your Firefox profile cannot be loaded. It may be missing or inaccessible.

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

I know that the profile is valid/compatible because I can successfully start the browser and profile with:

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

不过,我不知道如何向以这种方式打开的浏览器发送命令.

I don't know how to send commands to a browser opened in such a manner, however.

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

I've found similar questions, but I'm specifically looking for a Java solution, preferably tested on Windows.

我正在使用可以在这里下载的独立 Selenium 库和 Tor 浏览器包可以在这里下载.

I'm using a standalone Selenium library that can be downloaded here and the Tor Browser Bundle that can be downloaded here.

推荐答案

由于 Tor Browser Bundle 不允许我使用 WebDriver 扩展,我找到了一种解决方法,在该方法中我从常规 Firefox 浏览器运行 Tor.使用这种方法,只要打开 Tor 浏览器,就可以在普通的 Firefox 浏览器中使用 Tor.

Because Tor Browser Bundle wasn't letting me use the WebDriver extension, I found a workaround in which I ran Tor from a regular Firefox browser. With this method, as long as the Tor Browser is open, you can use Tor with a regular Firefox browser.

  • 打开 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.

  • Close browsers. Note that if you plan on doing a lot of closing and reopening (useful in obtaining a new IP address), I advise setting the profile preference toolkit.startup.max_resumed_crashes to a high value like 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 中使用 Selenium WebDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆