简体   繁体   English

在 selenium geckodriver 上设置代理

[英]Set up proxy on selenium geckodriver

I'm having issues adding proxy support to my geckodriver selenium program.我在向 geckodriver selenium 程序添加代理支持时遇到问题。

                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.第一次使用 geckodriver,我总是使用 chromedriver。

EDIT: In answer to @AtachiShadow , the issue remains.编辑:为了回答@AtachiShadow,问题仍然存在。

                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):我使用以下命令在浏览器中启用代理袜子(这是 Python 3 代码):

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() .然而,我通过.FirefoxProfile()而不是通过.Options()启用代理。

Try it like this.像这样试试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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