使用 Selenium 的 Java 和 Tor 浏览器

标签 java selenium browser geckodriver tor

我已经在网上搜索了几个小时的解决方案,但它们都已经过时或不起作用。

有人有一个可行的解决方案来连接 Java + Selenium + Tor 浏览器吗?

一个简短的例子就足够了。

我的最佳尝试,但出现“tor 无法启动”错误:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

import java.io.File;
import java.util.concurrent.TimeUnit;

public class JSelenium {

public static void main(String[] args) {
    System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
    System.setProperty("webdriver.firefox.marionette", "geckodriver.exe");
    FirefoxOptions options = new FirefoxOptions();
    FirefoxProfile torProfile = new FirefoxProfile(new File("xxx\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default"));

    options.setBinary("xxx\\Tor Browser\\Browser\\firefox.exe");
    options.setProfile(torProfile);
    options.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);

    WebDriver driver = new FirefoxDriver(options);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.navigate().to("http://www.google.com");
}

}

最佳答案

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

    options.setProfile(profile);

将其添加到您的 Java 代码中并打开新的 Firefox 浏览器。重要的是您需要运行一次 tor.exe。您可以在“Tor Browser\Browser\TorBrowser\Tor”下的 Tor 文件夹中找到它。

关于使用 Selenium 的 Java 和 Tor 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59329565/

相关文章:

css - 浏览器默认字体大小是否取决于 DPI?

java - 重写抽象方法 TimerTask.Run()

linux - 是否有任何 API 可用于获取 selenium 网格中可用的特定浏览器的插槽数?

java - 迭代 LinkedHashMap 并在新行上返回每个 ‘String’ 元素

java - 使用 selenium 测试注销

browser - 查找安全页面上的所有不安全内容

java - DynamoDB NoSQL 和无法实例化类的新实例

java - 使用外部 Https 主机进行 Spring Boot 测试的 WireMock

java - 您可以使用 GSON 的 JsonElement 对大多数但不是所有类成员执行 equals 比较吗

javascript - 在脚本/注释/隐藏 Div 中存储附加数据