From: Tomas G. <t_g...@ho...> - 2013-12-19 13:40:16
|
Hi 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 with your mouse 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); //I dont know if the javascript function uses ajax, but i use this because of tips from internet. webClient.setAjaxController(new NicelyResynchronizingAjaxController()); 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(); webClient.waitForBackgroundJavaScript(1000); System.out.println(page2.asXml()); The new page i get is the same as the original one, 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 get HtmlAnchor object from right cell HtmlAnchor anchor = (HtmlAnchor) cells.get(10).getFirstElementChild(); page2 = (HtmlPage) anchor.click(); webClient.waitForBackgroundJavaScript(1000); System.out.println(page2.asXml()); Like with previous try the new page i get is same as the original one with no extra info. Have i forgotten something? What else can i try to do? I'm not sure how and when to use to use waitForBackgroundJavaScript and setAjaxController I would really appreciate suggestions on how to solve this. Best regards /Tomas |