如何使用 Selenium RemoteWebDriver 设置 HTTP_PROXY?

问题描述

这是我当前在没有任何代理的情况下启动浏览器的代码


                properties = getGridProperties();
                DesiredCapabilities capabilities = null;
                FirefoxProfile profile = new FirefoxProfile();
                    profile.setPreference("layout.css.devPixelsPerPx","0.9");
                    FirefoxOptions options = new FirefoxOptions().setProfile(profile);
                    options.addPreference("dom.webnotifications.enabled",false);
                    if (properties.containsKey("hub.gecko.driver.path"))
                        System.setProperty("webdriver.gecko.driver",properties.getProperty("hub.gecko.driver.path"));
                    capabilities = DesiredCapabilities.firefox();
                    capabilities.setCapability(FirefoxDriver.PROFILE,profile);
                    if (browserType.equalsIgnoreCase("oldfirefox")) {
                        capabilities.setCapability("marionette",false);
                        capabilities.setCapability("platform","LINUX");
                        options.merge(capabilities);
                    }
                printInfo("initRemoteWebDriver:started:" + System.currentTimeMillis());
                capabilities.setCapability("idleTimeout",150);
                String nodeURL = "http://" + properties.getProperty("grid.hub.host") + "/wd/hub";
                capabilities.setCapability("idleTimeout",150);
                capabilities.setCapability("name",this.getClass().getCanonicalName());

                driver = new RemoteWebDriver(new URL(nodeURL),capabilities);
                setDriver(driver);
                getDriver().manage().window().maximize();
                printInfo("****Firefox browser launched***");
                printInfo("initRemoteWebDriver:finished:" + System.currentTimeMillis());

要设置的所需代理详细信息:

HTTP_PROXY = 'http://www-proxy.us.abc.com:80'
HTTPS_PROXY = 'http://www-proxy.us.abc.com:80'

在不过多更改当前代码的情况下,最简单的方法是什么?

解决方法

嗨,请试试这个

Proxy proxy = new Proxy();
proxy.setHttpProxy("http://www-proxy.us.abc.com:80");
capabilities.setCapability(CapabilityType.PROXY,proxy);

希望对你有用。 谢谢

,

请参考 Selenium 文档 - https://www.selenium.dev/documentation/en/webdriver/http_proxies/

,

另一种方法是使用 JVM 标志。这样您就完全不必更改代码了。

java -Dhttp.proxyHost=http://www-proxy.us.abc.com -Dhttp.proxyPort=80 -Dhttp.nonProxyHosts=”localhost|127.0.0.1|10.*.*.*”

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...