java脚本执行在 java 中失败,XPathResult 未定义

javascript execution failing in java with XPathResult undefined

我正在尝试使用 java 执行 java 脚本函数,但我收到一条错误消息,提示无法找到某些 classes .有人可以帮我解决这个问题吗?

我的Javaclass

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$RecompilationA$\^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)

如果需要我这边的数据,请告诉我。

您的代码的问题是,您使用的 XPathResult 存在于 window 对象下,window 对象实现由 Web 浏览器提供。 Nashorn (ScriptEngine) 不提供 window 实现。

来自 Java 文档:

While Oracle Nashorn runs ECMA-compliant JavaScript, it is important to note that objects normally accessible in a web browser are not available, for example, console, window, and so on.

您可以使用 Nashorn 使用 jjs 选项执行和验证您的 js 代码:

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

因为你想解析 HTML 文件,你可以使用 HTML 解析器来实现它,例如 https://jsoup.org/ ,如果正在使用此代码进行测试,你可以使用 Selenium 的 API JavascriptExecutor#executeScript(...)