簡體   English   中英

C# Selenium - 無法啟動 Tor

[英]C# Selenium - Failed to Start Tor

我正在嘗試使用以下代碼通過 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 Failed to Start ”,並且異常只包含“ 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");

重要筆記:

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

  • marionette.enabled : 真

  • marionette.port :設置為未使用的端口,並在代碼中將此值設置為firefoxDriverService.BrowserCommunicationPort 在我的示例中設置為 2828。

據我幾年前的嘗試記憶,當您設置“ 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);

您的代碼塊對我來說看起來很完美。

但是,打開使用默認Firefox v68.9.0esr Browser 9.5似乎存在問題。

您可以在如何使用 GeckoDriver 和 Selenium 到 ZA7F5F35426B6278211FCZ231B537 使用默認 Firefox 到 68.9.0esr 啟動 Tor 瀏覽器 9.5 中找到詳細討論


解決方案

解決方案是安裝和使用以下任一瀏覽器:

  • Firefox v77.0.1
  • Firefox 夜間版 v79.0a1

如果您使用上述瀏覽器,您需要:

  • Firefox v77.0.1

     String torPath = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
  • 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