4

I am using web client for getting page source. First time i am getting page source. After i use the same object for getting page source for different URL it's showing an Exception like:

java.lang.ClassCastException: com.gargoylesoftware.htmlunit.UnexpectedPage cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage

This is the code which i am using.

HtmlPage firstPage = webClient.getPage("firsturl");
HtmlPage downloadPage = null;
        try {
            webClient.setJavaScriptEnabled(true);

            downloadPage = (HtmlPage) webClient.getPage("secondurl");
        } catch (Exception e) {
            e.printStackTrace();
        }

Thx in advance

2 Answers 2

1

It is saying it quite clearly, Your code is doing:

downloadPage = (HtmlPage) webClient.getPage("secondurl");

so you assume you are getting an object of type HtmlPage, but you are actually getting an object of type UnexpectedPage.

You should add a check of instanceof:

If (webClient.getPage("secondurl") instanceof HtmlPage){
downloadPage = (HtmlPage) webClient.getPage("secondurl");
}
else{
//do something
}
Sign up to request clarification or add additional context in comments.

Comments

1

I assume (without knowing the library too well any more) that UnexpectedPage is a subtype of HtmlPage (if it isn't - that's the reason for your problem).

In this case you probably have these classes twice on the classpath. While the "name" of the class HtmlPage looks like the legitimate superclass, the classloader has access to two classes of the same name and loaded "the other" one first.

Check (double-check) for HtmlUnit classes appearing twice on your classpath.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.