That said clearly, your code does:
downloadPage = (HtmlPage) webClient.getPage("secondurl");
so you assume that you are getting an object of type HtmlPage , but in fact you are getting an object of type UnexpectedPage .
You should add an instanceof check:
If (webClient.getPage("secondurl") instanceof HtmlPage){ downloadPage = (HtmlPage) webClient.getPage("secondurl"); } else{
source share