2012-06-28 76 views

回答

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; 
    } 
}; 
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 
    } 
} 
相關問題