用Java中的Selenium webdriver运行Tor

时间:2016-08-29 17:09:49

标签: java selenium selenium-webdriver tor

我对硒有点新意,我希望用它来通过Tor运行各种自动化任务。我在Windows 10上使用Java通过Eclipse,将Selenium包作为引用库。我已经查看了其他几个教程和论坛帖子,详细介绍了如何在selenium中设置Tor。我开始尝试使用以下代码:

    System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
    String torPath = 
            "C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\firefox.exe";
    String profilePath = 
            "C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
    FirefoxProfile torProfile = new FirefoxProfile(new File(profilePath));
    FirefoxBinary binary = new FirefoxBinary(new File(torPath));
    WebDriver driver = new FirefoxDriver(binary, torProfile);
    driver.get("https://www.google.com");

使用该代码会产生此错误:

Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.

我尝试引用this thread for a solution。然后我使用以下代码基于该线程:

    System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
    String torPath = 
            "C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\firefox.exe";
    String profilePath = 
            "C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
    FirefoxProfile torProfile = new FirefoxProfile(new File(profilePath));
    FirefoxBinary binary = new FirefoxBinary(new File(torPath));
    torProfile.setPreference("webdriver.load.strategy", "unstable");
    try
    {
        binary.startProfile(torProfile, new File(profilePath), "");
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    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);
    WebDriver driver = new FirefoxDriver(binary, profile);
    driver.get("https://www.google.com");

使用该代码为我启动Tor;但是,我仍然收到与以前相同的错误,驱动程序无法控制Tor窗口。

我将不胜感激任何帮助或建议。

0 个答案:

没有答案