Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlUnit can't retrieve page after downloading a file

Tags:

java

htmlunit

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!

like image 941
oli206 Avatar asked Jan 19 '23 08:01

oli206


1 Answers

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.

like image 105
oli206 Avatar answered Feb 01 '23 03:02

oli206