从Selenium调用WebDriver for Tor

时间:2015-02-06 10:51:40

标签: java firefox selenium selenium-webdriver tor

我需要一个Tor的Webdriver实例才能从selenium启动google.com,Windows中的java代码并能够搜索目标字符串

我的代码在

下面
String torPath = "..Installations\\Tor\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "..Installations\\Tor\\TorBrowser\\Browser\\TorBrowser\\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浏览器页面打开。它不会根据需要加载google.com。我知道该配置文件是有效/兼容的,因为我可以使用以下命令成功启动浏览器和配置文件:

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

我看过类似的问题,但没有得到满意的答复。

可以吗?如果有,怎么样? 我正在寻找Java代码。

1 个答案:

答案 0 :(得分:0)

    FirefoxDriver firefoxDriver;
    File torProfileDir = new File("C:\\tor\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default");
    FirefoxBinary binary = new FirefoxBinary(new File( "C:\\tor\\Tor Browser\\Browser\\firefox.exe"));//C:\tor\Tor Browser\Browser
    FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
    torProfile.setPreference("webdriver.load.strategy", "unstable");
    binary.startProfile(torProfile, torProfileDir);

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

使用此代码