如何使用 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
}
}