简体   繁体   中英

Set up proxy on selenium geckodriver

I'm having issues adding proxy support to my geckodriver selenium program.

                var proxy = new Proxy();

            if (useproxies == true)
            {
                if (proxytype) //True = SOCKS5
                {
                    /*var proxy = proxies[proxyindex];
                    profile.SetPreference("network.proxy.type", 1);
                    profile.SetPreference("network.proxy.socks", proxy.Split(':')[0]);
                    profile.SetPreference("network.proxy.socks_port", proxy.Split(':')[1]);
                    */
                    proxy.SocksProxy = proxies[proxyindex];
                    if (proxyindex >= (proxies.Count - 1)) { proxyindex = 0; } else { proxyindex++; }
                }
                else //False = HTTP
                {
                    proxy.HttpProxy = proxies[proxyindex];
                    if (proxyindex >= (proxies.Count - 1)) { proxyindex = 0; } else { proxyindex++; }
                }
            }
            //user agent
            profile.SetPreference("general.useragent.override", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0");

            //start
            options.Proxy = proxy;
            options.Profile = profile;
            driver = new FirefoxDriver(options);

But this isn't working, I already tried a lot of ways, but none is working for me. Anyone knows how to do this? First time working with geckodriver, I always use chromedriver.

EDIT: In answer to @AtachiShadow , the issue remains.

                var profile = new FirefoxProfile();
            var options = new FirefoxOptions();

            //proxy
            if (useproxies == true)
            {
                if (proxytype) //True = SOCKS5 | False = HTTP
                {
                    try
                    {
                        profile.SetPreference("network.proxy.type", 1);
                        profile.SetPreference("network.proxy.socks", proxies[proxyindex].Split(':')[0]);
                        profile.SetPreference("network.proxy.socks_port", proxies[proxyindex].Split(':')[1]);
                        profile.SetPreference("network.proxy.socks_version", 5);
                        if (proxyindex >= (proxies.Count - 1)) { proxyindex = 0; } else { proxyindex++; }
                    }
                    catch
                    {
                        WriteConsole("Proxies.txt is bad... Exiting.");
                        Console.Read();
                        Environment.Exit(0);
                    }
                }
                else
                {
                    try
                    {
                      if (proxyindex >= (proxies.Count - 1)) { proxyindex = 0; } else { proxyindex++; }
                    }
                    catch
                    {
                        WriteConsole("Proxies.txt is bad... Exiting.");
                        Console.Read();
                        Environment.Exit(0);
                    }
                }

            }
            //user agent
            profile.SetPreference("general.useragent.override", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0");

            //start
            options.Profile = profile;
            driver = new FirefoxDriver(options);

Same issue with your sugestion

I enable proxy socks in my browser with the following commands (this is Python 3 code):

fp = webdriver.FirefoxProfile(ub_profile)
fp.set_preference('network.proxy.type', 1)  # int
fp.set_preference('network.proxy.socks', '111.11.11.1')  # string
fp.set_preference('network.proxy.socks_port', 12345)  # int
fp.set_preference('network.proxy.socks_version', 4)  # int
browser = webdriver.Firefox(firefox_profile=fp)

And there is a difference with your code. You probably don't indicate which version of the proxy socks you have:

fp.set_preference('network.proxy.socks_version', 4)  # When you have proxy version 4

or

fp.set_preference('network.proxy.socks_version', 5)  # When you have proxy version 5

And yet, I enable proxies through .FirefoxProfile() , and not through .Options() .

Try it like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM