如何从我的Java应用程序中设置Selenium的代理设置? [英] How to set from my Java application the proxy settings for Selenium?

查看:193
本文介绍了如何从我的Java应用程序中设置Selenium的代理设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to change the proxy of selenium server from my Java application. When I set the proxy the common way the Selenium server doesn't use this setting. I mean when I start the selenium browser and I go to an IP checking service (search google for "what is my ip") I want that the proxy IP to appear and not my IP address.

推荐答案

如果您在Selenium 2.0中使用 WebDriver API来控制浏览器,那么你可以使用 org.openqa.selenium.Proxy 用于定义代理的类,以及将其指定为功能 启动 WebDriver 实例时。 Selenium常见问题解答在一个问题中解决它

If you are using the WebDriver API in Selenium 2.0, to control the browser, you may configure the browser to use a proxy, using the org.openqa.selenium.Proxy class to define the proxy, and specify it as a Capability when starting the WebDriver instance. The Selenium FAQ addresses it in a question:


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

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

A: Proxy configuration is done via the org.openqa.selenium.Proxy class like so:

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);


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

If you are using Selenium RC (of Selenium 1; the API is available in Selenium 2 for backward compatibility), then you will need to configure Selenium Server to use the proxy. This is because Selenium Server is itself configured as the proxy for the browser, and therefore, it is Selenium Server that will have to forward the HTTP requests to the web-application via the proxy. Proxy details can be provided as JVM startup flags to Selenium Server, as noted in the Selenium documentation:


代理配置

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

If your AUT is behind an HTTP proxy which requires authentication then you should configure http.proxyHost, http.proxyPort, http.proxyUser and http.proxyPassword using the following command.

$ java -jar selenium-server-standalone-<version-number>.jar -Dhttp.proxyHost=proxy.com -Dhttp.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=password


这篇关于如何从我的Java应用程序中设置Selenium的代理设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆