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

可紊 提交于 2019-12-02 00:25:03

问题


How do I handle authentication with the HtmlUnitDriver?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/11242723/how-do-i-handle-authentication-with-the-htmlunitdriver-using-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!