简体   繁体   中英

How can I get the result from the JavaScript html page using HtmlUnit?

I'm now working on using a JavaScript html to do some tests. And I want to use HtmlUnit to access the page to acquire the running results on the JavaScript webpage. And this page is here(for the page source, you can right-click mouse to see the source):

http://itec.hust.edu.cn/~zhangcw/javascript.html

what I want is to get the running result about the delay value "delay:xxx ms" calculated by the JavaScript on that page. However, when I use HtmlUnit to capture the html, it always capture the original source of that page and doesn't contain the running result. My code is like this:

WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("http://itec.hust.edu.cn/~zhangcw/javascript.html");
String source = page.asXml();
System.out.println(Source);

The source only contains the JavaScript source codes and html content, not the execution result by the JavaScript on the page. What I should do using HtmlUnit?

My Question is how can I use the HtmlUnit to get the JavaScript running result on the page?

Thanks for reading the text, tough for my English skill.

HtmlUnit has a JavaScript engine that is very sensitive to syntactical errors. So the fact that something works in a normal browser (Chrome, IE, FF, etc) doesn't mean that it will work on HtmlUnit.

I took a very quick look at the code and detected too many syntactical errors. I even run this through the w3c validator and found 10 errors. Here is just one example:

<button type="button style="

You're clearly missing a quote there.

And also some errors that the validator might not detect such as:

<text id= _delay>

You are missing the quotes and you have an extra space.

Finally, the most likely reason why the code is not working is that you defined it as HTML 4.01 Transitional and you added the HTML5 attribute onerror to the image. Furthermore, you've added a piece of JavaScript to that attribute and it happens to be the JavaScript that is not executing.

Fixing errors might help. However, it doesn't necessarily mean it will work after that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM