我需要 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/