我已经在网上搜索了几个小时的解决方案,但它们都已经过时或不起作用。
有人有一个可行的解决方案来连接 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/