HTMLUnit 未使用 java 脚本正确加载页面

问题描述

问题是当使用 HTMLUnit 加载网页时,由于 java 脚本错误页面无法正确加载,是否有任何替代库可以更好地加载 javascript 或修复方法

    public static void main(String[] args) throws FailingHttpStatusCodeException,MalformedURLException,IOException,InterruptedException {
        WebClient webClient = new WebClient(browserVersion.FIREFOX);
        webClient.getoptions().setJavaScriptEnabled(true);
        webClient.getoptions().setCssEnabled(false);

        webClient.getoptions().setUseInsecureSSL(false); //ignore ssl certificate
        webClient.getoptions().setThrowExceptionOnScriptError(false);
        webClient.getoptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.setJavaScriptErrorListener(new SilentJavaScriptErrorListener());
        String url = "https://prices.runescape.wikI/Osrs/item/11834";
        webClient.getPage(url);
        webClient.waitForBackgroundJavaScriptStartingBefore(20000);
        HtmlPage myPage = (HtmlPage) webClient.getCurrentwindow().getEnclosedPage();
        System.out.println(myPage.asXml());


    }
    }

这是输出

<?xml version="1.0" encoding="UTF-8"?>
<html lang="en">
  <head>
    <Meta charset="utf-8"/>
    <link rel="icon" href="/favicon.png"/>
    <Meta name="viewport" content="width=device-width,initial-scale=1"/>
    <link rel="apple-touch-icon" href="/favicon.png"/>
    <link rel="manifest" href="/manifest.json"/>
    <script async="" src="https://www.googletagmanager.com/gtag/js?id=G-NDL25C1NDW">
    </script>
    <script>
//<![CDATA[
function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","G-NDL25C1NDW")
//]]>
    </script>
    <title>
      Prices from the OSRS Wiki
    </title>
    <link href="/static/css/main.ad22d35c.chunk.css" rel="stylesheet"/>
  </head>
  <body>
    <script>
//<![CDATA[
"serviceWorker"in navigator&&window.addEventListener("load",(function(){navigator.serviceWorker.register("worker.js").then((function(e){console.log("ServiceWorker registration successful with scope: ",e.scope)}),(function(e){console.log("ServiceWorker registration Failed: ",e)}))}))
//]]>
    </script>
    <noscript>
      This website requires JavaScript.
    </noscript>
    <div id="root">
    </div>
    <script>
//<![CDATA[
!function(e){function r(r){for(var n,i,p=r[0],l=r[1],a=r[2],f=0,s=[];f<p.length;f++)i=p[f],Object.prototype.hasOwnProperty.call(o,i)&&o[i]&&s.push(o[i][0]),o[i]=0;for(n in l)Object.prototype.hasOwnProperty.call(l,n)&&(e[n]=l[n]);for(c&&c(r);s.length;)s.shift()();return u.push.apply(u,a||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,p=1;p<t.length;p++){var l=t[p];0!==o[l]&&(n=!1)}n&&(u.splice(r--,1),e=i(i.s=t[0]))}return e}var n={},o={1:0},u=[];function i(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return e[r].call(t.exports,t,t.exports,i),t.l=!0,t.exports}i.m=e,i.c=n,i.d=function(e,r,t){i.o(e,r)||Object.defineProperty(e,{enumerable:!0,get:t})},i.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},i.t=function(e,r){if(1&r&&(e=i(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(i.r(t),Object.defineProperty(t,"default",value:e}),2&r&&"string"!=typeof e)for(var n in e)i.d(t,n,function(r){return e[r]}.bind(null,n));return t},i.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return i.d(r,"a",r),r},i.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},i.p="/";var p=this["webpackJsonpprices.runescape.wiki"]=this["webpackJsonpprices.runescape.wiki"]||[],l=p.push.bind(p);p.push=r,p=p.slice();for(var a=0;a<p.length;a++)r(p[a]);var c=l;t()}([])
//]]>
    </script>
    <script src="/static/js/2.3a7f2a4d.chunk.js">
    </script>
    <script src="/static/js/main.dfe6395a.chunk.js">
    </script>
  </body>
</html>

如果您转到 https://prices.runescape.wiki/osrs/item/11834 处的页面,您会看到与返回的 XML 结果相去甚远

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...