Htmlunit Class Cast Exception: Textpage Cannot Be Cast To Htmlpage
Solution 1:
The problem is that HTML Unit is not able to cast incompleted HTML Pages (some unclosing tags, for example). So, I could solve this error using HTMLParser which is included in HTMLUnit's packages (I'm using 2.36.0v). HTMLParser completes and handles this kind of casting errors.
//Web client creation.Pagepage= webClient.getPage(url);
HtmlPagetmpPage= HTMLParser.parseHtml(page.getWebResponse(), webClient.getCurrentWindow());
// use tmpPage here
Solution 2:
HtmlSubmitInputsubmitBtn= currentPage.getFirstByXPath("//input[@value='Search']");
currentPage = submitBtn.click();
Solution 3:
HtmlUnit doesn't care at all about the applicationContext.xml
or your Java server code. All it matters to it is the output the server generates. If the output is random text then HtmlUnit will assume it is fetching a text file and will create a TextPage to process it. If the output is HTML then HtmlUnit will assume it is getting an HTML file and create an HtmlPage to hold the result. Based on your question you clearly need the latter.
So verify the output from your server application (http://localhost:8080/htmlunitdemo/login
) and make sure it outputs valid HTML code.
Solution 4:
replace code line:
HtmlPagenewPage= submit.click();
with:
TextPagenewPage= submit.click();
Solution 5:
I do not know if I already find the solution. The error occurs when the button is Submit, not if it is a button type. I solved it by adding a button and executing it.
HtmlElementbuttonCustom= (HtmlElement) page.createElement("button");
buttonCustom.setAttribute("type", "submit");
buttonCustom.setAttribute("name", "submit");
buttonCustom.setAttribute("value", "Load");
form.appendChild(buttonCustom);
Post a Comment for "Htmlunit Class Cast Exception: Textpage Cannot Be Cast To Htmlpage"