java中javascript执行失败,xpathresult未定义

k4ymrczo  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(457)

我试图用java执行javascript函数,但得到一条错误消息,它无法找到某些类。有人能帮我解决这个问题吗?
我的java类

public class TestException {

    public static void main(String[] args) throws IOException, ScriptException {
        ScriptEngineManager engineMgr=new ScriptEngineManager();
        ScriptEngine engine=engineMgr.getEngineByName("JavaScript");
        Document doc=HtmlPage.getHTML("", "C:\\Users\\DELL\\Desktop\\PHPTRAVELS.html", "https://phptravels.com");
        String xpath="//input";

        //for single value
    /*  String jscript="return document.evaluate('"+xpath+"',document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;";
        WebElement element=(WebElement)jse.executeScript(jscript);
        System.out.println(element.getAttribute("name"));*/

        String multiCode="var test=function(document){"
                + "var results=document.evaluate('//input', document,null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);"+
                "var tagNames = [];\r\n"
                + "var count=0;" +
                "while(node = results.iterateNext()) {\r\n" + 
                "  count=count+1;" + 
                "}"
                + "\r\n return count}";
        /*FileWriter fileWrite=new FileWriter(new File(System.getProperty("user.dir")+"\\jScript.js"));
        fileWrite.write(multiCode);
        fileWrite.flush();
        fileWrite.close();*/

        try{
            engine.eval(multiCode);
            Invocable invc=(Invocable) engine;
            Long count=(Long)invc.invokeFunction("test", doc);
        //Long count=(Long) jse.executeScript(multiCode);
        System.out.println(count);
        }catch(Exception e){
            e.printStackTrace();
        }
        }

例外

javax.script.ScriptException: ReferenceError: "XPathResult" is not defined in <eval> at line number 1
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:392)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:190)
    at demo.TestException.main(TestException.java:58)
Caused by: <eval>:1 ReferenceError: "XPathResult" is not defined
    at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
    at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:319)
    at jdk.nashorn.internal.runtime.ECMAErrors.referenceError(ECMAErrors.java:291)
    at jdk.nashorn.internal.objects.Global.__noSuchProperty__(Global.java:1441)
    at jdk.nashorn.internal.scripts.Script$Recompilation$1$27A$\^eval\_.test(<eval>:1)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:639)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
    at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:199)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:386)

请让我知道如果数据需要从我这边。

ldxq2e6h

ldxq2e6h1#

代码的问题是,您正在使用 XPathResult 出现在 window 对象, window 对象实现由web浏览器提供。nashorn(scriptengine)不提供 window 实施。
来自java文档:
尽管oraclenashorn运行符合ecma的javascript,但需要注意的是,web浏览器中通常可访问的对象不可用,例如控制台、窗口等。
您可以使用nashorn执行和验证js代码 jjs 选项:

> jjs test.js
abc.js:2 ReferenceError: "XPathResult" is not defined

当您想要解析html文件时,您可以通过使用html解析器(如https://jsoup.org/ ,如果您使用此代码进行测试,则可以使用selenium的api JavascriptExecutor#executeScript(...)

相关问题