简体   繁体   English

如何使用HtmlUnit从JavaScript html页面获取结果?

[英]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. 我现在正在使用JavaScript html进行一些测试。 And I want to use HtmlUnit to access the page to acquire the running results on the JavaScript webpage. 我想使用HtmlUnit访问页面以获取JavaScript网页上的运行结果。 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 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. 我想要的是获取有关该页面上的JavaScript计算的延迟值“ delay:xxx ms”的运行结果。 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. 但是,当我使用HtmlUnit捕获html时,它总是捕获该页面的原始源,并且不包含运行结果。 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. 源仅包含JavaScript源代码和html内容,而不包含页面上JavaScript的执行结果。 What I should do using HtmlUnit? 使用HtmlUnit应该怎么做?

My Question is how can I use the HtmlUnit to get the JavaScript running result on the page? 我的问题是如何使用HtmlUnit在页面上获取JavaScript运行结果?

Thanks for reading the text, tough for my English skill. 感谢您阅读课文,我的英语能力很强。

HtmlUnit has a JavaScript engine that is very sensitive to syntactical errors. HtmlUnit具有一个JavaScript引擎,该引擎对语法错误非常敏感。 So the fact that something works in a normal browser (Chrome, IE, FF, etc) doesn't mean that it will work on HtmlUnit. 因此,某些东西可以在普通浏览器(Chrome,IE,FF等)中运行并不意味着它可以在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. 我什至通过w3c验证程序运行它,发现10个错误。 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. 最后,代码无法正常工作的最可能的原因是您将其定义为HTML 4.01 Transitional ,并将HTML5属性onerror添加到了图像。 Furthermore, you've added a piece of JavaScript to that attribute and it happens to be the JavaScript that is not executing. 此外,您已经向该属性添加了一段JavaScript,而恰好是未执行的JavaScript。

Fixing errors might help. 修复错误可能会有所帮助。 However, it doesn't necessarily mean it will work after that. 但是,这并不一定意味着它将在那之后工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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