From: Sreenadh VP <sre...@co...> - 2014-02-05 10:20:16
|
Hi, Thanks for your attention and response. I have following code WebRequest request = new WebRequest(new URL(Url)); HtmlPage page = webClient.getPage(request); HtmlButton elementTocick = (HtmlButton)page.getElementById("elementId"); page = elementTocick.click(); // fails with java.lang.ClassCastException: com.gargoylesoftware.htmlunit.TextPage cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage Need to assign new page after click to* page* reference of type HtmlPage, how can do the the same? Please advice Sreenadh On Wed, Feb 5, 2014 at 2:49 PM, smk smk <ste...@gm...> wrote: > Hi > > As Sreenadh mentioned not every response is an HtmlPage object however all > of them are Page objects. Thus you have to typecast accordingly. > > Check the following code wchich returns the source as String according to > the type of the response. > > Page pageSource = webClient.getPage("http://example.com"); > String pageSourceString = getPageSource(pageSource); > > public static String getPageSource(Page page) > { > if(page instanceof HtmlPage) > { > return ((HtmlPage)page).asXml(); > } > else if(page instanceof JavaScriptPage) > { > return ((JavaScriptPage)page).getContent(); > } > else if(page instanceof TextPage) > { > return ((TextPage)page).getContent(); > } > else //page instanceof UnexpectedPage > { > return ((UnexpectedPage)page).getWebResponse().getContentAsString(); > } > } > > From the above example you can see that there are 4 different types of > Pages that webClient returns. > > The method used here returns the string of the source however you can > modify the code to your needs according to the instance is returned. > > Stergios > > > > > > 2014-02-05 Ahmed Ashour <asa...@ya...>: > >> Hi, >> >> This happens when HtmlUnit determines the content type of this page to be >> plain text, not HTML. >> >> Please send the details of the complete case. >> >> Ahmed >> ------------------------------ >> *From:* Sreenath <sre...@gm...> >> *To:* "htm...@li..." < >> htm...@li...> >> *Sent:* Wednesday, February 5, 2014 11:53 AM >> *Subject:* [Htmlunit-user] java.lang.ClassCastException: >> com.gargoylesoftware.htmlunit.TextPage cannot be cast to >> com.gargoylesoftware.htmlunit.html.HtmlPage >> >> Hello, >> >> I have tried to load pme web page url and did submission of the page, >> second page is not getting loaded due to following issue. >> >> >> java.lang.ClassCastException: com.gargoylesoftware.htmlunit.TextPage >> cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage >> >> Note: my page is type of HtmlPage, not able to cast to HtmlPage >> >> Please advice why the second page is coming as type of >> com.gargoylesoftware.htmlunit.TextPage, and how can assign to HtmlPage >> type the same. >> >> Thanks in Advance >> Sreenadh >> >> >> >> ------------------------------------------------------------------------------ >> Managing the Performance of Cloud-Based Applications >> Take advantage of what the Cloud has to offer - Avoid Common Pitfalls. >> Read the Whitepaper. >> >> http://pubads.g.doubleclick.net/gampad/clk?id=121051231&iu=/4140/ostg.clktrk >> _______________________________________________ >> Htmlunit-user mailing list >> Htm...@li... >> https://lists.sourceforge.net/lists/listinfo/htmlunit-user >> >> > > > ------------------------------------------------------------------------------ > Managing the Performance of Cloud-Based Applications > Take advantage of what the Cloud has to offer - Avoid Common Pitfalls. > Read the Whitepaper. > > http://pubads.g.doubleclick.net/gampad/clk?id=121051231&iu=/4140/ostg.clktrk > _______________________________________________ > Htmlunit-user mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/htmlunit-user > > |