Adding proxy settings in Chrome browser with c# and Selenium

Asked

Viewed 299 times

2

I need to open the Chrome browser with Tor proxy settings using selenium. I was successful with firefox but it’s not very agile, so you need to use Chrome. The problem is I don’t know how to apply the settings to Chrome.

I need a working solution for the cromedriver.

in firefox 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;
            }

        }

1 answer

2

After a lot of research I was able to solve my problem that was simpler than I imagined, because I now intended how the port configuration, custom options and local host ip proxy tor in Selenium using proxy tor 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 proxy tor and pass a localhost ip along with the port Docker exposes to the client, with the container started I call this method above to apply the proxy settings and other additional as view mode of the browser with window and without window where runs all processes without an interface, after all this return a chromeoption object and apply to my driver.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.