如何使用selenium在PhantomJS中设置代理身份验证? [英] How to set proxy authentication in PhantomJS using selenium?

查看:907
本文介绍了如何使用selenium在PhantomJS中设置代理身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中运行这个简单的selenium测试:

I'm running this simple selenium test in java:

public static void main(String[] args){
    WebDriver driver = new PhantomJSDriver();
    driver.get("http://www.google.com");
    WebElement element = driver.findElement(By.id("gbqfif"));
    element.sendKeys("cheese");
    element.submit();
    System.out.println("Titulo:"+driver.getTitle());
    driver.quit();
}

但是在我的办公室它需要代理身份验证,我不知道如何设置它。

but here at my office it requires proxy authentication and I have no idea how to set it.

我必须把我的用户和密码放在某个地方。

I have to put my user and password somewhere.

你能帮帮我吗? / p>

Can you help me out?

推荐答案

PhantomJS使用从命令行设置的三个代理选项( docs )。

PhantomJS uses the three proxy options that are set from the commandline (docs).



  • - proxy = address:port 指定要使用的代理服务器(例如 - proxy = 192.168.1.42:8080 )。

  • - proxy-type = [http | socks5 | none] 指定代理服务器的类型(默认为 http )。

  • - proxy-auth 指定身份验证信息代理人,例如 - proxy-auth =用户名:密码)

  • --proxy=address:port specifies the proxy server to use (e.g. --proxy=192.168.1.42:8080).
  • --proxy-type=[http|socks5|none] specifies the type of the proxy server (default is http).
  • --proxy-auth specifies the authentication information for the proxy, e.g. --proxy-auth=username:password).

要使用这些,您必须将它们添加到DesiredCapabilities地图(如答案中所示):

To use these, you have to add them to the DesiredCapabilities map (as seen in this answer):

ArrayList<String> cliArgsCap = new ArrayList<String>();
cliArgsCap.add("--proxy=address:port");
cliArgsCap.add("--proxy-auth=username:password");
cliArgsCap.add("--proxy-type=http");
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setCapability(
    PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
WebDriver driver = new PhantomJSDriver(capabilities);

这篇关于如何使用selenium在PhantomJS中设置代理身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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