I have one HTML file where a JavaScript method (say) postIt()
is defined inside the <script>
tag. Its tag is preceded by two references to external JavaScript files which no longer exist. All these <script>
tags are inside <body>
.
I had to set WebClient.setThrowExceptionOnFailingStatusCode
to false, so that I can proceed with the non-existent JavaScript files.
But when I try calling page.executeJavascript("postIt()")
, I get Reference not found error!
- What is the reason for this and do we have any possible workaround?
- Does the JavaScript engine run in its o开发者_JAVA百科wn thread?
Most JavaScript engines stop parsing/evaluating code when they encounter parsetime/compiletime errors, just like other compilers.
This is a feature, not a bug.
The workaround is to fix the errors.
Well, as it turns out, I was invoking incorrect JavaScript code which in turn redirected my code to a 404 page. But since I had set webClient.setThrowExceptionOnFailingStatusCode()
and webClient.setPrintContentOnFailingStatusCode()
to false, HtmlUnit didn't notify me about this.
So for these kind of issues we need to keep the above flags turned on, and remove ALL JavaScript errors.
精彩评论