Selenium WebDriver 更改 Firefox 路径为 Tor

3
我正在尝试将Ruby中的Webdriver更改为打开Tor浏览器而不是默认的Firefox浏览器。我正在使用以下代码,并且在运行此代码之前已经打开了Tor浏览器。
path='C:\Users\Bonnnie\Downloads\Tor Browser\App\tor.exe'
Selenium::WebDriver::Firefox.path = path
driver = Selenium::WebDriver.for :firefox

I get the following error:

unable to obtain stable firefox connection in 60 seconds

我觉得我可能链接了错误的Tor文件。


1
你正在使用哪个selenium-webdriver版本和FireFox版本? - Arup Rakshit
火狐浏览器 v22.0 似乎和 Webdriver v2.35 兼容。 - Richard Friedman
我不知道你的意思,但是我确认:Firefox v22.0和Webdriver v2.35。 - Richard Friedman
你可能想查看我在http://stackoverflow.com/questions/18511136/how-to-open-tor-browser-using-watir/19250405#19250405上的答案。 - IceyEC
2个回答

1
以下方法适用于我使用的selenium-webdriver 2.48.1和Tor浏览器套件5.0.3在Ubuntu Linux 15.04上。
require 'selenium-webdriver'

tor_dir = '/opt/tor-browser_en-US'
# The Tor binary relies on these shared libraries
ENV['LD_LIBRARY_PATH']= [
  File.join(tor_dir, 'Browser/'),
  File.join(tor_dir, 'Browser/TorBrowser/Tor/')
].join(':')
Selenium::WebDriver::Firefox::Binary.path =
  File.join(tor_dir, 'Browser/firefox')
profile = Selenium::WebDriver::Firefox::Profile.new(
  File.join(tor_dir, 'Browser/TorBrowser/Data/Browser/profile.default'))
driver = Selenium::WebDriver.for :firefox, :profile => profile
driver.get('https://check.torproject.org/')

尝试在Ubuntu 20.04上使用TBB 10.5.10和selenium-webdriver 4.0.3运行此代码。必须将Selenium :: WebDriver :: Firefox :: Binary.path = File.join(tor_dir,'Browser / firefox')替换为Selenium :: WebDriver :: Firefox :: Service.driver_path = File.join(tor_dir,'Browser / firefox')。 TBB启动,但随后出现超时错误:unable to connect to /opt/tor-browser_en-US/Browser/firefox 127.0.0.1:4445 (Selenium::WebDriver::Error::WebDriverError)。我已经将Tor作为服务在我的系统上运行,但即使停止服务也会得到相同的结果。有什么好的建议吗?也许与Marionette相关? - MatzFan

0
require 'selenium-webdriver'

tor_dir = '/home/me/tor-browser_en-US' # change as necessary
options = Selenium::WebDriver::Firefox::Options.new
options.binary = File.join(tor_dir, 'Browser/firefox')
proxy = Selenium::WebDriver::Proxy.new(socks: '127.0.0.1:9050')
proxy.socks_version = 5
options.proxy = proxy
driver = Selenium::WebDriver.for :firefox, options: options
driver.get('https://check.torproject.org')
driver.title
=> "Congratulations. This browser is configured to use Tor."

我不得不设置浏览器代理以使用我本地运行的Tor,因为Selenium显然不使用Tor Browser Bundle捆绑的代理。如果没有设置代理,我会收到“Selenium :: WebDriver :: Error :: UnknownError:Reached error page: about:neterror?e = proxyConnectFailure”的错误信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接