title: Unable to initialize "document" in this code
body: <p>I'm trying to call a javascript function from java using selenium web driver.</p>
  
  <p>here is the code</p>
  
  <pre><code>WebDriver driver = new FirefoxDriver();
  
  driver.get("C:\\KIRAN\\TrialAndError.htm"); // The html page is loaded here
  
  // Following is the way i am trying to run the javascript
  
  ScriptEngineManager manager = new ScriptEngineManager();
  
  ScriptEngine engine = manager.getEngineByName("javascript");
  
  String script = "function getInputIds(param) {  
                            print(\"inside getInputIds\");    
                            var elems = document.form[0].elements;    
                            var length = elems.length;    
                            var arr = new Array();  
                            var match_found = 0;  
                            for (i = 0; i &lt; elems.length; i++) {      
                                 match_found = elems[i].id.match(/order_unit_line_rate_\\d+_unit/);       
                                 if (match_found) {           
                                    arr.push(elems[i].id);        
                                 }    
                             }    
                             print(elems[1].id);}";
  
  engine.eval(script);
  
  Invocable inv = (Invocable) engine; 
  
  inv.invokeFunction("getInputIds", "Scripting!!" );
  </code></pre>
  
  <p>But this the error i get when i run this code</p>
  
  <pre><code>inside getInputIdsException : javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "document" is not defined. (&lt;Unknown source&gt;#1) in &lt;Unknown source&gt; at line number 1
  </code></pre>
  
  <p>Please help me run this piece of code by initializing the "document" object.</p>
  
  <p>Also let me know if it is possible to return the array "arr" to be used in the java file outside the javascript.</p>
  
  <p>Thanks in Advance
  Kiran</p>