numX
numX

Reputation: 850

C# Selenium - Failed to Start Tor

I'm trying to launch Tor browser through Selenium in C# using the following code:

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'. 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.

Any help is very much appreciated.

Update - Solved: After updating to the latest Tor version (9.5.1) The final working code:

            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 :

Upvotes: 2

Views: 1573

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193348

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.

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


Solution

The solution will be to install and use either of the following browsers:

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

In case you use the above mentioned browsers, you need to:

  • 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";
    

Upvotes: 1

Noldor
Noldor

Reputation: 1182

As far as I remember from my attempts a few years ago, TOR with WebDriver didn't work when you set that "Profile" option. Normal Firefox works, but TOR simply doesn't.

If you get a timeout error after this, make sure marionette is enabled on about:config. If it's already enabled, follow what is going on with TOR on start up. 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. 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);

Upvotes: 3

Related Questions