【问题标题】:TypeError: Cannot find function entries in object function Object()TypeError:在对象函数 Object() 中找不到函数条目
【发布时间】:2021-05-20 19:52:49
【问题描述】:

我正在使用 htmlunit 来获取这个网页

https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies

Maven 依赖:

<dependency>
      <groupId>net.sourceforge.htmlunit</groupId>
      <artifactId>htmlunit</artifactId>
      <version>2.49.1</version>
</dependency>

我收到以下错误

Exception in thread "main" ======= EXCEPTION START ========
EcmaError: lineNumber=[2] column=[0] lineSource=[<no source>] name=[TypeError] sourceName=[https://developer.mozilla.org/static/js/2.6b2c0865.chunk.js] message=[TypeError: Cannot find function entries in object function Object() {
    [native code]
}. (https://developer.mozilla.org/static/js/2.6b2c0865.chunk.js#2)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function entries in object function Object() {
    [native code]
}. (https://developer.mozilla.org/static/js/2.6b2c0865.chunk.js#2)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:949)
        at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:650)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:540)
        at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:353)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:829)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:805)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:110)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:1022)
        at com.gargoylesoftware.htmlunit.html.ScriptElementSupport.executeScriptIfNeeded(ScriptElementSupport.java:196)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.executeDeferredScriptsIfNeeded(HtmlPage.java:1474)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:259)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:677)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:559)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:474)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:386)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:521)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:502)
        at rpvs.WebScraping.main(WebScraping.java:88)
Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot find function entries in object function Object() {
    [native code]
}. (https://developer.mozilla.org/static/js/2.6b2c0865.chunk.js#2)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4396)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4374)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4407)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeErrorById(ScriptRuntime.java:4413)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:4520)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2619)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2601)
        at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1543)
        at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:1013)
        at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:111)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:430)
        at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:339)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3665)
        at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:123)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$2.doRun(JavaScriptEngine.java:820)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:934)
        ... 17 more
Enclosed exception:
net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot find function entries in object function Object() {
    [native code]
}. (https://developer.mozilla.org/static/js/2.6b2c0865.chunk.js#2)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4396)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4374)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4407)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeErrorById(ScriptRuntime.java:4413)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:4520)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2619)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2601)
        at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1543)
        at script.e(https://developer.mozilla.org/static/js/2.6b2c0865.chunk.js:2)
        at script(https://developer.mozilla.org/static/js/2.6b2c0865.chunk.js:2)
        at script.f(https://developer.mozilla.org/static/js/runtime-main.930f84d6.js:1)
        at script(https://developer.mozilla.org/static/js/main.e9dd68ce.chunk.js:1)
        at script.f(https://developer.mozilla.org/static/js/runtime-main.930f84d6.js:1)
        at script.r(https://developer.mozilla.org/static/js/runtime-main.930f84d6.js:1)
        at script.t(https://developer.mozilla.org/static/js/runtime-main.930f84d6.js:1)
        at script(https://developer.mozilla.org/static/js/main.e9dd68ce.chunk.js:1)
        at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:1013)
        at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:111)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:430)
        at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:339)
        at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3665)
        at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:123)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$2.doRun(JavaScriptEngine.java:820)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:934)
        at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:650)
        at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:540)
        at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:353)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:829)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:805)
        at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:110)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:1022)
        at com.gargoylesoftware.htmlunit.html.ScriptElementSupport.executeScriptIfNeeded(ScriptElementSupport.java:196)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.executeDeferredScriptsIfNeeded(HtmlPage.java:1474)
        at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:259)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:677)
        at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:559)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:474)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:386)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:521)
        at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:502)
        at rpvs.WebScraping.main(WebScraping.java:88)
======= EXCEPTION END ========

这是我的源代码:

import java.io.IOException;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;



public class WebScraping {
    public static void main(String[] args) throws IOException {
        
        WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED);

        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setUseInsecureSSL(true);

        HtmlPage page = webClient.getPage("https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies");

        String pageText = page.asNormalizedText();
        if(pageText != null){
            pageText = pageText.toLowerCase();

            System.out.println(pageText);
        }
      
        webClient.close();
    }

}

我不知道是什么导致了错误以及如何解决它。

【问题讨论】:

    标签: java htmlunit htmlunit-driver


    【解决方案1】:

    当前的 Rhino 实现似乎存在问题。幸运的是,Rhino 开发取得了一些进展——您缺少的功能似乎正在开发中 (https://github.com/mozilla/rhino/pull/902)

    一旦它登陆 Rhino,我将更新 core-js 并提供一个新的快照。请查看推特 (https://twitter.com/htmlunit) 以了解更新。

    【讨论】:

    • 谢谢。当我禁用 javascript webClient.getOptions().setJavaScriptEnabled(false); 时,我没有收到错误消息。通过这样做,我还可以获取页面的规范化文本。
    猜你喜欢
    • 1970-01-01
    • 2018-06-04
    • 2015-09-27
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多