1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I will check errors. And I am puzzled whether I call htmlunit in a right way to execute JavaScript on html just like the codes I attached in question.
It's OK the way it is (provided Source is all lowercased) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.