java 如何使用 selenium 在 PhantomJS 中设置代理身份验证?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26931565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:54:02  来源:igfitidea点击:

How to set proxy authentication in PhantomJS using selenium?

javaproxyselenium-webdriverwebdriverphantomjs

提问by Bruno Sousa

I'm running this simple selenium test in java:

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

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.

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

Can you help me out?

你能帮我吗?

回答by Artjom B.

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

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

  • --proxy=address:portspecifies 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-authspecifies the authentication information for the proxy, e.g. --proxy-auth=username:password).
  • --proxy=address:port指定要使用的代理服务器(例如--proxy=192.168.1.42:8080)。
  • --proxy-type=[http|socks5|none]指定代理服务器的类型(默认为http)。
  • --proxy-auth指定代理的身份验证信息,例如--proxy-auth=username:password)

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

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

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