开发者

How to force HtmlUnit to save page?

开发者 https://www.devze.com 2022-12-29 06:10 出处:网络
I\'m using HtmlUnit to click on a HtmlElement that triggers Javascript action: currentPage = ((HtmlElement) currentPage.get开发者_运维知识库ByXPath(\"//*[contains(@onclick, \'check();\')]\").get(0)).

I'm using HtmlUnit to click on a HtmlElement that triggers Javascript action:

currentPage = ((HtmlElement) currentPage.get开发者_运维知识库ByXPath("//*[contains(@onclick, 'check();')]").get(0)).click();

The element is:

<a href="#" onclick="check(); return false;">

The page returned is quite similar to the page, containing that element: same URL, mostly same HTML, but there are some minor differences in the HTML and HtmlUnit doesn't save the new page. I'm using HttpAnalyzer to sniff the traffic and I see that the Webclient correctly handles JS and send the right request. The response is also correct, but when I dump the contents of the currentPage to a file, I see that the actual page didn't change. How can I fix it?


I had the same problem. In my case it was:

<div id="loginButton" class="buttonOuter" onclick="this.childNodes[1].click();">
<div class="buttonInner">
<a href="#">Login</a>
</div>
<input class="loginHiddenButton" type="submit" onclick="submit_proc(false,'')">
</div>

I solve it this way:

HtmlPage page1 = webClient.getPage("myURI");
HtmlElement htmlElement = (HtmlElement) page1.getByXPath("loginHiddenButtonXPATH").get(0);

webClient.setJavaScriptEnabled(false);
page1 = htmlElement.click();
webClient.setJavaScriptEnabled(true);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号