最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Prevent HtmlUnit 2.13 from executing JavaScript - Stack Overflow

programmeradmin5浏览0评论

Here is my code to get the page:

WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage(url);

The problem is the webClient always executes javascript automatically and throws me a list of error. I just want to get the raw source. How can I prevent it from executing script? I've found there is a way in version 2.9:

webClient.setJavaScriptEnabled(false);

But setJavaScriptEnabled() function was deprecated. Anyone knows how to solve this problem? Please help me. Thank you so much.

Here is my code to get the page:

WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage(url);

The problem is the webClient always executes javascript automatically and throws me a list of error. I just want to get the raw source. How can I prevent it from executing script? I've found there is a way in version 2.9:

webClient.setJavaScriptEnabled(false);

But setJavaScriptEnabled() function was deprecated. Anyone knows how to solve this problem? Please help me. Thank you so much.

Share Improve this question asked Nov 18, 2013 at 10:52 Triet DoanTriet Doan 12.1k9 gold badges39 silver badges75 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 22

Although setJavaScriptEnabled(boolean) was deprecated it was added to the WebClientOptions member of the WebClient. Here is the doc.

In order to disable JavaScript you should do this:

webClient.getOptions().setJavaScriptEnabled(false);

Additionally, if you you want to get the raw HTML code from the webpage you should take a look at this question:

How to get the pure raw HTML of a page in HTMLUnit while ignoring JavaScript and CSS?

Take into account that even the asXml() method change the formatting as well as the content of the web page you fetch (even if JavaScript is disabled).

发布评论

评论列表(0)

  1. 暂无评论