2

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 :

  • marionette.enabled: true

  • marionette.port: set to an unused port, and set this value to firefoxDriverService.BrowserCommunicationPort in your code. This was set to 2828 in my example.

numX
  • 830
  • 7
  • 24
  • I tried the same and I get the error - `OpenQA.Selenium.WebDriverException : Process unexpectedly closed with status 1`. Am I missing anything? – fingers10 Jan 08 '23 at 18:09

2 Answers2

3

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);
Noldor
  • 1,122
  • 9
  • 22
  • @numX posted a new answer. my selenium driver version was Selenium.WebDriver Version="3.141.0" at that time – Noldor Jun 30 '20 at 14:02
  • I tried this and it still didn't work, however I'll be trying some variations of it in the coming days. If you're able to get it working it would be very much appreciated! – numX Jul 03 '20 at 06:01
  • After updating to the latest Tor version and changing the marionette configurations this seems to have worked! I'll be adding the working code for reference. – numX Jul 05 '20 at 17:07
  • @Noldor I tried the same and I get the error - `OpenQA.Selenium.WebDriverException : Process unexpectedly closed with status 1`. Am I missing anything? – fingers10 Jan 08 '23 at 18:12
  • hey @Noldor, thanks for the solution! Too much `var` in the snippet but it is the easiest and most straightforward solution after searching a day, thanks! – alexDuty Jun 03 '23 at 08:42
1

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";
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks for your reply @DebanjanB , if I replace Tor with firefox won't that compromise anonymity? Or are you implying that if I change the exec path but retain the same profile it should be secure? – numX Jul 03 '20 at 06:00
  • @numX _Tor_ based on _Mozilla_ is basically for anonymity. The default implementation is through **Firefox v68.9.0esr**. However you can always use the other browser flavors from Mozilla where the browsing engine is same. See: [How to initiate a Tor Browser 9.5 which uses the default Firefox to 68.9.0esr using GeckoDriver and Selenium through Python](https://stackoverflow.com/questions/62666075/how-to-initiate-a-tor-browser-9-5-which-uses-the-default-firefox-to-68-9-0esr-us) – undetected Selenium Jul 03 '20 at 06:06
  • Ah I see that you ran into a similar issue, upvoted ! I didn't fully understand your comment though, replacing the tor exe with the FF versions that you mention won't achieve the same level of anonymity right? – numX Jul 03 '20 at 15:49
  • @numX The fact is _TOR_ project is released bundled with _FirefoxESR_ by default. But main aim is anonymity which can be achieved by implementing _TOR_ project through different browsers based on _Mozilla Firefox_ and _Chromium_ browsing engine. – undetected Selenium Jul 03 '20 at 20:17
  • Doesn't the FF exe used by TOR have additional privacy patches bundled in, or is it the vanilla FF ESR? – numX Jul 05 '20 at 16:01
  • managed to resolve this after updating to the latest Tor and changing some of its configs, updates the answer with the full code. Thanks for your help! – numX Jul 05 '20 at 17:19