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

时间:2012-06-28 10:31:44

标签: java selenium webdriver htmlunit-driver

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

2 个答案:

答案 0 :(得分: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
  }
} 

答案 1 :(得分:2)

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