How to add Tor proxy settings in chrome browser with C# and selenium

I need to open chrome browser with Tor proxy settings using Selenium. I got success with firefox but it is not very agile, so you have to use chrome. The problem is that I do not know how to apply the settings for chrome.

I need a functional solution for cromedriver.

In firefox I did so, this method is called after the TOR BROWSER is opened:

private void ConfigureBrowser()
        {
          Logger.Log($"{Logger.MethodName()} -- Configurando Firefox...");
            try
            {
                var ffOptions = new FirefoxOptions();
                ffOptions.SetPreference("network.proxy.type", 1);
                ffOptions.SetPreference("network.proxy.socks", "127.0.0.1");
                ffOptions.SetPreference("network.proxy.socks_port", 9150);
                ffOptions.AcceptInsecureCertificates = true;

                var t =  @"WebDrivers\";
                Logger.Log($"{Logger.MethodName()} PATH GECKO -> {t}", ConsoleColor.DarkMagenta);
                this.DRIVER = new FirefoxDriver(t, ffOptions);
                this.Wait = new WebDriverWait(this.DRIVER, TimeSpan.FromSeconds(80));
                Logger.Log($"{Logger.MethodName()}  FIREFOX OK", ConsoleColor.DarkGreen);
            }
            catch (System.Exception e)
            {
                Logger.Log($"{Logger.MethodName()}  ERRO AO INICIAR FIREFOX -> {e.ToString()}", ConsoleColor.DarkRed);
                throw;
            }

        }
Author: Sérgio Lopes, 2018-12-28

1 answers

After a lot of research I managed to solve my problem which was simpler than I imagined, because now I intended how the port configuration, custom options and local IP host of tor proxy in selenium worked using tor proxy and chrome browser.

Follows below my configuration method:

    public ChromeOptions GetDriveOptions()
    {

        var options = new ChromeOptions();

        options.AddUserProfilePreference("download.prompt_for_download", false);
        options.AddUserProfilePreference("download.directory_upgrade", true);
        options.AddUserProfilePreference("download.default_directory", PathDowload);
        options.AddArgument("--no-sandbox");

        if (HeadLess)
            options.AddArgument("--headless");

        if (UseTor)
            options.AddArgument("--proxy-server=socks5://" + "127.0.0.1" + ":" + "9050");

        options.AcceptInsecureCertificates = true;

        return options;
    }

I use a docker container to run the Tor proxy and pass a localhost ip along with the port that docker exposes to the client, with the container started I call this method above to apply proxy settings and other additional As browser view with window and without window where it runs all processes without an interface, after all this I return a chromeoption object and apply to my driver.

 2
Author: MisterNBr, 2019-04-04 17:31:55