线程“main”中的异常javax.script.ScriptException:sun.org.mozilla.javascript.EcmaError:ReferenceError:“document”未定义

时间:2014-08-04 09:34:56

标签: javascript java javax.script

我想做以下事情

  1. 使用Selenium打开网址
  2. 使用ScriptEngine,想要执行一个Javascript函数,该函数在www.google.co.in上提供所有锚点标记:

    package public;
    
    import javax.script.Invocable;
    import javax.script.ScriptEngine;
    import javax.script.ScriptEngineManager;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class InvokeScriptFunction
    {
        public static void main(String[] args) throws Exception {
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("JavaScript");
            WebDriver driver=new FirefoxDriver();
            driver.get("http://www.google.co.in");
    
            // JavaScript code in a String
                 String script = "function hello() { print(document.anchors); }";
    
            // evaluate script
            engine.eval(script);
    
            // javax.script.Invocable is an optional interface.
            // Check whether your script engine implements or not!
            // Note that the JavaScript engine implements Invocable interface.
            Invocable inv = (Invocable) engine;
    
            // invoke the global function named "hello"
           inv.invokeFunction("hello" );
        }
    }
    
  3. 我想通过这个程序访问DOM元素。如果我只是打印一个字符串然后它的工作正常,但给出以下异常:

    Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.EcmaError: ReferenceError: "document" is not defined. (<Unknown source>#1) in <Unknown source> at line number 1
        at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:300)
        at com.sun.script.javascript.RhinoScriptEngine.invokeFunction(RhinoScriptEngine.java:258)
        at publicc.InvokeScriptFunction.main(InvokeScriptFunction.java:32)
    Caused by: sun.org.mozilla.javascript.EcmaError: ReferenceError: "document" is not defined. (<Unknown source>#1)
        at sun.org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3785)
        at sun.org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3763)
        at sun.org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3848)
        at sun.org.mozilla.javascript.ScriptRuntime.name(ScriptRuntime.java:1781)
        at sun.org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1780)
        at sun.org.mozilla.javascript.Interpreter.interpret(Interpreter.java:854)
        at sun.org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164)
        at sun.org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:426)
        at com.sun.script.javascript.RhinoScriptEngine$1.superDoTopCall(RhinoScriptEngine.java:116)
        at com.sun.script.javascript.RhinoScriptEngine$1.doTopCall(RhinoScriptEngine.java:109)
        at sun.org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3178)
        at sun.org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:162)
        at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:294)
        ... 2 more
    

0 个答案:

没有答案