如何使用Selenium WebDriver处理HtmlUnitDriver的身份验证



如何使用 HtmlUnitDriver 处理身份验证?

在 java 中尝试这个似乎对我有用

WebDriver driver = new HtmlUnitDriver() {
    protected WebClient modifyWebClient(WebClient client) {
        // This class ships with HtmlUnit itself
        DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
        // Set some example credentials
        creds.addCredentials("username", "password");
        // And now add the provider to the webClient instance
        client.setCredentialsProvider(creds);
        return client;
    }
};

如果这是您需要的基本身份验证,则可以在创建 HtmlUnitDriver 实例时执行此操作:(代码在 scala 中,但您可以轻松地将其更改为 java)

new HtmlUnitDriver() {
  override def modifyWebClient(client: WebClient) = {
    val creds = new DefaultCredentialsProvider()
    creds.addCredentials("user-name", "user-password");
    client.setCredentialsProvider(creds)
    client
  }
} 

相关内容

  • 没有找到相关文章

最新更新