【问题标题】:C# Selenium - Failed to Start TorC# Selenium - 无法启动 Tor
【发布时间】:2020-10-20 01:40:13
【问题描述】:

我正在尝试使用以下代码在 C# 中通过 Selenium 启动 Tor 浏览器:

using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AutomatorApp
{
  public class BrowserAutomator
  {
     public void Automate()
     {

        String torPath = "D:\\Tor Browser\\Browser\\firefox.exe";
        String profilePath = "D:\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default\\";

        FirefoxProfile profile = new FirefoxProfile(profilePath);
        profile.SetPreference("network.proxy.type", 1);
        profile.SetPreference("network.proxy.socks", "127.0.0.1");
        profile.SetPreference("network.proxy.socks_port", 9153);
        profile.SetPreference("network.proxy.socks_remote_dns", false);

        FirefoxDriverService firefoxDriverService = FirefoxDriverService.CreateDefaultService("D:\\geckodriver-v0.26.0-win64", "geckodriver.exe");
        firefoxDriverService.FirefoxBinaryPath = torPath;

        var firefoxOptions = new FirefoxOptions
        {
            Profile = profile,
            LogLevel = FirefoxDriverLogLevel.Trace
        };

        FirefoxDriver driver = new FirefoxDriver(firefoxDriverService, firefoxOptions);
      }
   }
}

但是,这会显示错误“Tor 无法启动”,并且该异常仅包含“Permission denied”。我尝试以管理员模式启动应用程序并确保所有用户都可以访问该文件夹,但这并没有解决问题。

有趣的是,当我尝试使用相同的设置启动 Firefox 浏览器时,它运行良好。

非常感谢任何帮助。

更新 - 已解决:更新到最新的 Tor 版本(9.5.1)后最终工作代码:

            FirefoxProfile profile = new FirefoxProfile(profilePath);
            profile.SetPreference("network.proxy.type", 1);
            profile.SetPreference("network.proxy.socks", "127.0.0.1");
            profile.SetPreference("network.proxy.socks_port", 9153);
            profile.SetPreference("network.proxy.socks_remote_dns", false);

            FirefoxDriverService firefoxDriverService = FirefoxDriverService.CreateDefaultService(geckoDriverDirectory);
            firefoxDriverService.FirefoxBinaryPath = torPath;
            firefoxDriverService.BrowserCommunicationPort = 2828;
            var firefoxOptions = new FirefoxOptions
            {
                Profile = null,
                LogLevel = FirefoxDriverLogLevel.Trace
            };
            firefoxOptions.AddArguments("-profile", profilePath);
            FirefoxDriver driver = new FirefoxDriver(firefoxDriverService, firefoxOptions);
            driver.Navigate().GoToUrl("https://www.google.com");

重要提示:

以下 TOR 配置需要在 about:config 中进行更改:

  • ma​​rionette.enabled:真

  • ma​​rionette.port:设置为未使用的端口,并在您的代码中将此值设置为 firefoxDriverService.BrowserCommunicationPort。在我的示例中设置为 2828。

【问题讨论】:

    标签: c# selenium firefox geckodriver tor


    【解决方案1】:

    据我几年前的尝试,当您设置“Profile”选项时,带有 WebDriver 的 TOR 不起作用。普通的 Firefox 可以工作,但 TOR 根本不行。

    如果在此之后出现超时错误,请确保在 about:config 上启用了 marionette。如果它已经启用,请按照启动时 TOR 的情况进行操作。就像实际的Firefox浏览器没有加载,卡在连接启动器等,或者浏览器启动时有一个消息框等等......并且还要确保没有其他firefox.exegeckodriver.exetor.exe在后台运行。如果多个可执行文件未配置为监听不同的端口号,可能会导致问题。

        Environment.SetEnvironmentVariable("webdriver.gecko.driver", "C:\\TorBrowser\\Browser\\geckodriver.exe");
        var gekcoService = FirefoxDriverService.CreateDefaultService("C:\\TorBrowser\\Browser", "geckodriver.exe");
        var gekcoService.FirefoxBinaryPath = "C:\\TorBrowser\\Browser\\firefox.exe";
    
        // marionette port that browser listens to. I had it set to a custom port on about:config
        var gekcoService.BrowserCommunicationPort = 50111; 
    
        // also had given a custom port for geckodriver listen port
        var gekcoService.Port = 9881; 
        var gekcoService.Host = 127.0.0.1;
        var gekcoService.HostName = 127.0.0.1;
        var gekcoService.Start();
        
        var ffOptions = new FirefoxOptions
        {
            AcceptInsecureCertificates = true,
            BrowserExecutableLocation = "C:\\TorBrowser\\Browser\\firefox.exe",
            Profile = null, 
            UnhandledPromptBehavior = UnhandledPromptBehavior.Dismiss
        };
        
        ffOptions.AddArguments("-profile", "C:\\TorBrowser\\Browser\\TorBrowser\\Data\\Browser\\profile.default");
        ffOptions.LogLevel = FirefoxDriverLogLevel.Info;
        ffOptions.PageLoadStrategy = PageLoadStrategy.Eager;
        var ffDriver = new FirefoxDriver(gekcoService, ffOptions);
        ffDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10);
        ffDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(10);
        ffDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
    

    【讨论】:

    • @numX 发布了一个新答案。我当时的 selenium 驱动版本是 Selenium.WebDriver Version="3.141.0"
    • 我试过了,但还是不行,不过我会在接下来的几天里尝试一些变体。如果您能够使其正常工作,将不胜感激!
    • 在更新到最新的 Tor 版本并更改木偶配置后,这似乎奏效了!我将添加工作代码以供参考。
    【解决方案2】:

    您的代码块对我来说看起来很完美。

    但是,打开使用默认 Firefox v68.9.0esr Browser 9.5 似乎存在问题。

    您可以在How to initiate a Tor Browser 9.5 which uses the default Firefox to 68.9.0esr using GeckoDriver and Selenium through Python找到详细讨论


    解决方案

    解决方案是安装和使用以下任一浏览器:

    • Firefox v77.0.1
    • Firefox Nightly v79.0a1

    如果您使用上述浏览器,您需要:

    • Firefox v77.0.1

      String torPath = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
      
    • Firefox Nightly v79.0a1

      String torPath = "C:\\Program Files\\Firefox Nightly\\firefox.exe";
      

    【讨论】:

    • 感谢您的回复 @DebanjanB ,如果我用 firefox 替换 Tor 会不会影响匿名性?或者您是否暗示如果我更改执行路径但保留相同的配置文件它应该是安全的?
    • @numX Tor 基于 Mozilla 基本上是为了匿名。默认实现是通过 Firefox v68.9.0esr。但是,您始终可以使用浏览引擎相同的 Mozilla 的其他浏览器风格。见:How to initiate a Tor Browser 9.5 which uses the default Firefox to 68.9.0esr using GeckoDriver and Selenium through Python
    • 啊,我看到你遇到了类似的问题,赞成!不过,我并没有完全理解您的评论,用您提到的 FF 版本替换 tor exe 不会达到相同的匿名程度,对吧?
    • @numX 事实上,TOR 项目默认与 FirefoxESR 捆绑发布。但主要目的是匿名,可以通过基于 Mozilla FirefoxChromium 浏览引擎的不同浏览器实现 TOR 项目来实现。
    • TOR 使用的 FF exe 是不是捆绑了额外的隐私补丁,还是普通的 FF ESR?
    猜你喜欢
    • 1970-01-01
    • 2016-12-02
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多