java - 从 Selenium 调用 Tor 的 WebDriver

标签 java firefox selenium selenium-webdriver tor

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

我的代码如下

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 代码。

最佳答案

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

使用此代码

关于java - 从 Selenium 调用 Tor 的 WebDriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28364092/

相关文章:

java - 什么是 firebase 以及如何在 Android 中使用它?

html - 为什么 Firefox font-family 渲染比 Chrome 可怕?

google-chrome - 为 Protractor 安装特定的 chromedriver

java - 如何将 Spring Boot application.properties 外部化到 tomcat/lib 文件夹

java - Swing - 无法使用模态 JDialog 在 Windows 中切换语言

java - 如何在 TomEE 中配置 CXF

html - Firefox 中的全尺寸 IFrame?

html - 需要在 html anchor 上单击两次才能转到 id 或名称

java - Selenium - 等待网络流量

Selenium : "role hub"和 "role webdriver"之间的区别?