【问题标题】:Can't use TorBrowser with Selenium ? (Python Linux)不能将 TorBrowser 与 Selenium 一起使用? (Python Linux)
【发布时间】:2022-02-09 10:03:44
【问题描述】:

我想通过 selenium 运行 TorBrowser。

我已经能够使用 tor 守护程序和 firefox 实例通过 selenium 使用 tor 网络。

我希望使用 TorBrowser 能够使用不同的 Tor 出口中继运行多个实例。 我知道可以通过将此行添加到 Browser/TorBrowser/Data/Browser/profile.default/user.js强>:

user_pref("network.proxy.socks_port", ChangeToTheDesiredPort1);
user_pref("extensions.torlauncher.control_port", ChangeToTheDesiredPort2);

这是我用来尝试通过 Selenium 启动 TorBrowser 的代码。我正在尝试一步一步地做这些事情,所以在这个测试中,我使用了一个没有个性化配置文件的全新 TorBrowser 存档:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

profile = FirefoxProfile("tor-browser_en-US/Browser/TorBrowser/Data/Browser/profile.default")
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9050)

options = FirefoxOptions()
options.profile = profile
binary = FirefoxBinary("tor-browser_en-US/Browser/start-tor-browser")

print "0"
driver = webdriver.Firefox(options=options, firefox_binary=binary)
print "1"
driver.get('https://check.torproject.org/')

一旦我尝试实例化 webdriver,我的脚本就会被阻止。 脚本的输出打印 0,从不打印 1,并且 TorBrowser 从不尝试连接到 https://check.torproject.org/

如果我替换

binary = FirefoxBinary("tor-browser_en-US/Browser/start-tor-browser")

通过

binary = FirefoxBinary("tor-browser_en-US/Browser/firefox")

脚本不再阻塞,TorBrowser 尝试联系https://check.torproject.org/,但 TorBrowser 从未连接到 Tor 网络,导致以下错误:

selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=proxyConnectFailure&u=https://check.torproject.org/&c=UTF-8&d=Firefox 配置为使用拒绝连接的代理服务器。

关于我的配置的一些信息(64 位):

  • geckodriver 0.30.0
  • TorBrowser 11.0.4
  • Python 2.7.17
  • Ubuntu 18.04.1

我已使示例脚本尽可能简单,但过去 2 天我测试了很多东西,但没有发现任何相关内容。

提前感谢您的回答。

【问题讨论】:

    标签: python linux selenium tor-browser-bundle


    【解决方案1】:

    我终于解决了我的问题(在具有 Ubuntu 20.04 的 VM 上进行了测试,能够安装 selenium4)。 我已经能够使用 tbselenium(需要 selenium4)启动多个具有不同退出节点的 TorBrowser 实例。 https://github.com/webfp/tor-browser-selenium

    这是一个示例代码

    from stem.control import Controller
    from tbselenium.tbdriver import TorBrowserDriver
    import tbselenium.common as cm
    from tbselenium.utils import launch_tbb_tor_with_stem
    from selenium.webdriver.common.utils import free_port
    import tempfile
    from os.path import join
    import time
    
    
    tbb_dir = "PathToTorBrowserBundle"
    gecko = "PathToGeckodriver"
    
    socks_port = free_port()
    control_port = free_port()
    tor_data_dir = tempfile.mkdtemp()
    torrc = {'ControlPort': str(control_port),
            'SOCKSPort': str(socks_port),
            'DataDirectory': tor_data_dir}
    tor_binary = join(tbb_dir, cm.DEFAULT_TOR_BINARY_PATH)
    tor_process = launch_tbb_tor_with_stem(tbb_path=tbb_dir, torrc=torrc, tor_binary=tor_binary)
    
    Controller.from_port(port=control_port).authenticate()
    driver = TorBrowserDriver(tbb_dir, socks_port=socks_port, control_port=control_port, tor_cfg=cm.USE_STEM, executable_path=gecko)
    driver.load_url("https://check.torproject.org")
    time.sleep(5000)
    tor_process.kill()
    

    【讨论】:

      猜你喜欢
      • 2013-07-11
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 2018-11-21
      • 2014-04-23
      • 2013-07-19
      • 2016-05-31
      • 2015-01-15
      相关资源
      最近更新 更多