如何从我的Java应用程序中设置Selenium的代理设置?

edi*_*i66 4 java selenium

我想从我的Java应用程序中更改selenium服务器的代理.当我设置代理时,Selenium服务器不使用此设置的常见方式.我的意思是当我启动selenium浏览器并进入IP检查服务(搜索谷歌搜索"我的IP是什么")时,我希望代理IP出现而不是我的IP地址.

Vin*_*lds 5

如果您WebDriver在Selenium 2.0 中使用API来控制浏览器,您可以将浏览器配置为使用代理,使用org.openqa.selenium.Proxy类来定义代理,并将其指定为Capability启动WebDriver实例的时间.该硒FAQ解决它在一个问题:

问:我需要使用代理.我该如何配置?

答:代理配置是通过org.openqa.selenium.Proxy类完成的,如下所示:

Proxy proxy = new Proxy();
proxy.setProxyAutoconfigUrl("http://youdomain/config");

// We use firefox as an example here.
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.PROXY, proxy);

// You could use any webdriver implementation here
WebDriver driver = new FirefoxDriver(capabilities);
Run Code Online (Sandbox Code Playgroud)

如果您使用Selenium RC(Selenium 1;为了向后兼容性而在Selenium 2中提供API),那么您将需要配置Selenium Server以使用代理.这是因为Selenium Server本身被配置为浏览器的代理,因此,Selenium Server必须通过代理将HTTP请求转发到Web应用程序.代理详细信息可以作为JVM启动标志提供给Selenium Server,如Selenium文档中所述:

代理配置

如果您的AUT位于需要身份验证的HTTP代理之后,则应使用以下命令配置http.proxyHost,http.proxyPort,http.proxyUser和http.proxyPassword.

$ java -jar selenium-server-standalone-<version-number>.jar -Dhttp.proxyHost=proxy.com -Dhttp.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=password
Run Code Online (Sandbox Code Playgroud)