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

后端 未结 2 612
迷失自我
迷失自我 2021-01-21 09:57

How do I handle authentication with the HtmlUnitDriver?

相关标签:
2条回答
  • 2021-01-21 10:22

    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
      }
    } 
    
    0 讨论(0)
  • 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;
        }
    };
    
    0 讨论(0)
提交回复
热议问题