且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-09-18 16:58:34

PhantomJS 使用从命令行设置的三个代理选项 (文档).

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=username:password).
  • --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 映射(如 this 答案中所示):

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

推荐文章