如何使用Selenium运行Tor浏览器?

时间:2017-10-31 10:41:02

标签: selenium tor tor-browser-bundle

我正在尝试通过Selenium自动测试Tor浏览器,但到目前为止我无法让它工作。我的所有尝试都是以下代码的某种变体:

FirefoxProfile profile = new FirefoxProfile(new File("/Users/MyUsername/Library/Application Support/TorBrowser-Data/Browser/x1v4coki.default"));
profile.setPreference("webdriver.load.strategy", "unstable");
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.socks", "127.0.0.1");
profile.setPreference("network.proxy.socks_port", 9150);
FirefoxBinary binary = new FirefoxBinary(new File("/Applications/TorBrowser.app/Contents/MacOS/firefox"));
FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
options.setProfile(profile);
FirefoxDriver driver = new FirefoxDriver(options);

执行最后一行时,Tor浏览器成功启动。但是,程序将挂起一分钟左右,最终将抛出以下异常:

Exception in thread "main" org.openqa.selenium.WebDriverException: connection refused
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T15:28:36.4Z'
System info: host: '...', ip: '...', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.6', java.version: '1.8.0_144'
Driver info: driver.version: FirefoxDriver

当我省略profile.setPreference(...)行时,抛出相同的异常。 我使用的是Tor Browser 7.0.8,geckodriver 0.19.0和Selenium 3.6。

有人可以帮我找出导致异常的原因吗?

1 个答案:

答案 0 :(得分:0)

使用geckodriver 0.18.0而不是0.19.0解决了我的问题。