简体   繁体   中英

How do I handle authentication with the HtmlUnitDriver using Selenium WebDriver?

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

If that is the basic authentication that you need you can do this when creating a HtmlUnitDriver instance: (the code is in scala, but you can easily change it to java)

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

Try this in java seemed to work for me

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM