简体   繁体   English

C# Selenium - 无法启动 Tor

[英]C# Selenium - Failed to Start Tor

I'm trying to launch Tor browser through Selenium in C# using the following code:我正在尝试使用以下代码通过 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);
      }
   }
}

However, this shows the error ' Tor Failed to Start ' and the exception simply contains ' Permission denied '.但是,这显示了错误“ Tor Failed to Start ”,并且异常只包含“ Permission denied ”。 I tried launching the app in administrator mode and ensuring that the folder is accessible by all users but this did not solve the issue.我尝试以管理员模式启动应用程序并确保所有用户都可以访问该文件夹,但这并没有解决问题。

Interestingly, when I try to launch a Firefox browser using the same setup it works well.有趣的是,当我尝试使用相同的设置启动 Firefox 浏览器时,它运行良好。

Any help is very much appreciated.很感谢任何形式的帮助。

Update - Solved : After updating to the latest Tor version (9.5.1) The final working code:更新 -解决:更新到最新的 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");

Important notes:重要笔记:

The following TOR configs need to be changed in about:config :需要在about:config中更改以下 TOR 配置:

  • marionette.enabled : true marionette.enabled : 真

  • marionette.port : set to an unused port, and set this value to firefoxDriverService.BrowserCommunicationPort in your code. marionette.port :设置为未使用的端口,并在代码中将此值设置为firefoxDriverService.BrowserCommunicationPort This was set to 2828 in my example.在我的示例中设置为 2828。

As far as I remember from my attempts a few years ago, TOR with WebDriver didn't work when you set that " Profile " option.据我几年前的尝试记忆,当您设置“ Profile ”选项时,带有 WebDriver 的 TOR 不起作用。 Normal Firefox works, but TOR simply doesn't.正常 Firefox 工作,但 TOR 根本没有。

If you get a timeout error after this, make sure marionette is enabled on about:config .如果在此之后出现超时错误,请确保在about:config上启用了marionette If it's already enabled, follow what is going on with TOR on start up.如果它已经启用,请按照启动时 TOR 的情况进行操作。 Like if the actual firefox browser doesn't load up, stuck at connection launhcer etc, or there is a message box at the browser startup or something... And also make sure no other firefox.exe , geckodriver.exe or tor.exe is running on the background.就像实际的 firefox 浏览器无法加载,卡在连接启动器等,或者在浏览器启动时出现消息框等等......并且还要确保没有其他firefox.exegeckodriver.exetor.exe在后台运行。 If multiple executubles are not configured to listen different port numbers, it might cause problems.如果多个可执行文件未配置为侦听不同的端口号,则可能会导致问题。

    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);

Your code block looks perfect to me.您的代码块对我来说看起来很完美。

However there seems to be an issue opening the Browser 9.5 which uses the default Firefox v68.9.0esr .但是,打开使用默认Firefox v68.9.0esr Browser 9.5似乎存在问题。

You can find a detailed discussion in How to initiate a Tor Browser 9.5 which uses the default Firefox to 68.9.0esr using GeckoDriver and Selenium through Python您可以在如何使用 GeckoDriver 和 Selenium 到 ZA7F5F35426B6278211FCZ231B537 使用默认 Firefox 到 68.9.0esr 启动 Tor 浏览器 9.5 中找到详细讨论


Solution解决方案

The solution will be to install and use either of the following browsers:解决方案是安装和使用以下任一浏览器:

  • Firefox v77.0.1 Firefox v77.0.1
  • Firefox Nightly v79.0a1 Firefox 夜间版 v79.0a1

In case you use the above mentioned browsers, you need to:如果您使用上述浏览器,您需要:

  • Firefox v77.0.1 : Firefox v77.0.1

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM