Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6792875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:01:43+00:00 2026-05-26T18:01:43+00:00

I’m having this weird problem with HtmlUnit in Java. I am using it to

  • 0

I’m having this weird problem with HtmlUnit in Java. I am using it to download some data from a website, the process is something like this:

1 – Login

2 – For each element (cars)

—– 3 Search for car

—– 4 Download zip file from a link

The code:

Creation of the webclient:

webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setJavaScriptEnabled(true);
webClient.setThrowExceptionOnScriptError(false);
DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
provider.addCredentials(USERNAME, PASSWORD);
webClient.setCredentialsProvider(provider);
webClient.setRefreshHandler(new ImmediateRefreshHandler());

Login:

  public void login() throws IOException
  {
    page = (HtmlPage) webClient.getPage(URL);
    HtmlForm form = page.getFormByName("formLogin");

    String user = USERNAME;
    String password = PASSWORD;

    // Enter login and password
    form.getInputByName("LoginSteps$UserName").setValueAttribute(user);
    form.getInputByName("LoginSteps$Password").setValueAttribute(password);

    // Click Login Button
    page = (HtmlPage) form.getInputByName("LoginSteps$LoginButton").click();

    webClient.waitForBackgroundJavaScript(3000);

    // Click on Campa area
    HtmlAnchor link = (HtmlAnchor) page.getElementById("ctl00_linkCampaNoiH");
    page = (HtmlPage) link.click();

    webClient.waitForBackgroundJavaScript(3000);
    System.out.println(page.asText());
  }

Search for car in website:

private void searchCar(String _regNumber) throws IOException
 {
// Open search window
page = page.getElementById("search_gridCampaNoi").click();

webClient.waitForBackgroundJavaScript(3000);

// Write plate number
HtmlInput element = (HtmlInput) page.getElementById("jqg1");
element.setValueAttribute(_regNumber);

webClient.waitForBackgroundJavaScript(3000);

// Click on search
HtmlAnchor anchor = (HtmlAnchor) page.getByXPath("//*[@id=\"fbox_gridCampaNoi_search\"]").get(0);
page = anchor.click();

webClient.waitForBackgroundJavaScript(3000);
System.out.println(page.asText());
}

Download pdf:

    try
    {
      InputStream is = _link.click().getWebResponse().getContentAsStream();
      File path = new File(new File(DOWNLOAD_PATH), _regNumber);
      if (!path.exists())
      {
        path.mkdir();
      }
      writeToFile(is, new File(path, _regNumber + "_pdfs.zip"));
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

The problem:

The first car works okay, pdf is downloaded, but as soon as I search for a new car, when I get to this line:

page = page.getElementById("search_gridCampaNoi").click();

I get this exception:

Exception in thread "main" java.lang.ClassCastException: com.gargoylesoftware.htmlunit.UnexpectedPage cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage

After debugging, I’ve realized that the moment I make this call:

InputStream is = _link.click().getWebResponse().getContentAsStream();

the return type of page.getElementById(“search_gridCampaNoi”).click() changes from HtmlPage to WebResponse, so instead of receiving a new page, I’m receiving again the file that I already downloaded.

A couple of screenshots of the debugger showing this situation:

First call, return type OK:

enter image description here

Second call, return type changed and I no longer receive a HtmlPage:

enter image description here

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:01:43+00:00Added an answer on May 26, 2026 at 6:01 pm

    Just in case someone encounters the same problem, I found a workaround.Changing the line:

    InputStream is = _link.click().getWebResponse().getContentAsStream();
    

    to

    InputStream is = _link.openLinkInNewWindow().getWebResponse().getContentAsStream();
    

    seems to do the trick. Im having problems now when doing several iterations, sometimes it works, sometimes it doesn’t but at least I have something now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have some data like this: 1 2 3 4 5 9 2 6
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.