如何处理使用硒的webdriver的HtmlUnitDriver认证?(How do I handl

2019-06-25 21:03发布

如何处理与HtmlUnitDriver认证?

Answer 1:

在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;
    }
};


Answer 2:

如果这是你创建HtmlUnitDriver实例时需要你能做到这一点的基本身份验证:(代码是Scala,但你可以很容易地将其更改为JAVA)

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


文章来源: How do I handle authentication with the HtmlUnitDriver using Selenium WebDriver?