-1

I want to run the onclick javascript function, on the html code shown below, with java and HtmlUnit and get the info that the scripts produce.

<td width="13"><a class="img_attachfile16" alt="show files" title="show files" rel="Hide files" href="#" onclick="return (DocumentFiles.ShowHideFiles(12, 345234, 0, this))"></a></td>

What function ShowHideFiles do is that when you click on the link it will show you names of files(in the table cell) that can be downloaded and their urls. I want to get this information with java and HtmlUnit but i cant get it to work. This is what i've tried:

webClient = new WebClient(BrowserVersion.FIREFOX_17);
HtmlPage page = webClient.getPage(url);
List tables = page3.getByXPath("//table");
//Get the right table
HtmlTable table = (HtmlTable) tables.get(11);
List<HtmlTableCell> cells = tableRow.getCells();
//Get the right cell and its childelement(where the onclick function is).
HtmlElement element = cells.get(10).getFirstElementChild();

String clickAttr = element.getOnClickAttribute();
ScriptResult scriptResult = page.executeJavaScript(clickAttr);

//Get new page
HtmlPage page2 = (HtmlPage) scriptResult.getNewPage();
System.out.println(page2.asXml());

The new page i get is the same as before, meaning that i don't see the extra info i want. I've also tried to execute the click from the HtmlElement like this:

//Same as before but now make HtmlAnchor object from right cell
HtmlAnchor anchor = (HtmlAnchor) cells.get(10).getFirstElementChild();
page2 = (HtmlPage) anchor.click();
System.out.println(page2.asXml());

Like with previous try the new page i get is same as before with no extra info.

I would really appreciate suggestions on how to solve this.

Best regards /Tomas


I added this lines:

webClient.setAjaxController(new NicelyResynchronizingAjaxController());
//This one after getNewPage()
webClient.waitForBackgroundJavaScript(1000);

No changes in the new retrieved page now either. How do i get the error logs? I don't get any exceptions. What more can i do?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Tom
  • 139
  • 2
  • 6

2 Answers2

1

I will have to assume that you're performing an AJAX request with that code. If that is the case then it is most likely that the issue is that you're not waiting for the page to update.

Most likely this question will help:

Get the changed HTML content after it's updated by Javascript? (htmlunit)

Also make sure you aren't getting any error in the log.

Community
  • 1
  • 1
Mosty Mostacho
  • 42,742
  • 16
  • 96
  • 123
0

Once try this, it may work.

List<HtmlAnchor> anchors = new ArrayList<HtmlAnchor>();
            anchors = homePage.getAnchors();
            HtmlPage tempPage = null;
            for (int i = 0; i < anchors.size(); i++) {
                tempPage = anchors.get(i).click();
                String secondLink = homePage.getUrl().toString();
                System.out.println(secondLink);
                client.waitForBackgroundJavaScript(10000);
                client.waitForBackgroundJavaScriptStartingBefore(10000);
            }
Gaurab Pradhan
  • 281
  • 1
  • 5
  • 14