Search code examples
javaseleniumwebdriverhtmlunit-driver

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


How do I handle authentication with the HtmlUnitDriver?


Solution

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