我在使用htmlUnit WebDriver解析HTML页面时遇到了一些问题。 我没有例外。 我的代码如下:
public static void main(String[] args)
throws FailingHttpStatusCodeException, MalformedURLException,
IOException {
WebClient wc = initWebClient();
HtmlPage page = wc.getPage(Constants.START_PAGE);
HtmlTextInput userInput = (HtmlTextInput) page
.getElementById(Constants.INPUT_USERNAME_ID);
userInput.setText(Constants.USERNAME_VALUE);
HtmlPasswordInput passwordInput = (HtmlPasswordInput) page
.getElementById(Constants.INPUT_PASSWORD_ID);
passwordInput.setText(Constants.PASSWORD_VALUE);
// get submit button
HtmlSubmitInput submitButton = (HtmlSubmitInput) page
.getElementById(Constants.SUBMIT_BUTTON_ID);
HtmlPage afterLoginPage = submitButton.click();
System.out.println(afterLoginPage.asXml());
// some further processing
....
}
private static WebClient initWebClient(){
WebClient wc = new WebClient(BrowserVersion.CHROME);
wc.getCookieManager().setCookiesEnabled(true);
wc.getOptions().setJavaScriptEnabled(false);
wc.getOptions().setThrowExceptionOnScriptError(true);
System.out.println("USE SSL");
wc.getOptions().setUseInsecureSSL(true);
return wc;
}
在上面的XML源代码中,我可以找到我的名字。所以,我似乎正确地记录了它。
如果我已登录,那么我想通过在WebClient中设置其网址来转到页面。
for(some loop){
// create new WebClient, because it is MULTITHREADING processing. WebDriver is not thread safe so I need to create new WebClient for every thread
WebClient wc = initWebClient();
String url = "https://website/details/31944";
HtmlPage detailsPage = wc.getPage(url);
System.out.println(url);
System.out.println(detailsPage.getUrl());
}
以上sysout将返回:
https://website/details/31944
http://website/details/31944
这意味着当我转到https://website/details/31944时,我会收到http://website/details/31944,所以我不再登录了。
创建新的WebClient时,是否可以通过SSL会话? 或者也许是使用WebClient进行多线程处理的其他方法?
最好的问候,DS
答案 0 :(得分:1)
我已经使用cookies解决了这个问题。
public static void main(String[] args)
throws FailingHttpStatusCodeException, MalformedURLException,
IOException {
WebClient wc = initWebClient(null);
HtmlPage page = wc.getPage(Constants.START_PAGE);
HtmlTextInput userInput = (HtmlTextInput) page
.getElementById(Constants.INPUT_USERNAME_ID);
userInput.setText(Constants.USERNAME_VALUE);
HtmlPasswordInput passwordInput = (HtmlPasswordInput) page
.getElementById(Constants.INPUT_PASSWORD_ID);
passwordInput.setText(Constants.PASSWORD_VALUE);
// get submit button
HtmlSubmitInput submitButton = (HtmlSubmitInput) page
.getElementById(Constants.SUBMIT_BUTTON_ID);
HtmlPage afterLoginPage = submitButton.click();
Set<Cookie> cookies = wc.getCookieManager().getCookies();
....
....
// for every thread I create new WebClient
for(threads loop){
WebClient wc2 = initWebClient(cookies);
}
}
private static WebClient initWebClient(Set<Cookie> cookies) {
WebClient wc = new WebClient(BrowserVersion.CHROME);
wc.getCookieManager().setCookiesEnabled(true);
wc.getOptions().setJavaScriptEnabled(false);
wc.getOptions().setThrowExceptionOnScriptError(false);
if (cookies != null) {
Iterator<Cookie> i = cookies.iterator();
while (i.hasNext()) {
wc.getCookieManager().addCookie(i.next());
}
}
return wc;
}