title: using java variable within javascript - webDriver
body: <p>The alert is printing <code>[object]</code> and not the value of <code>elemID</code> for each iteration.</p>
  
  <pre><code>for (WebElement el : listOfElem){
      String elemID = el.getAttribute("id");
      Javascriptexecutor js = (Javascriptexecutor) driver;
      String strg = (String) js.executeScript("alert("+elemID+");");
  }
  </code></pre>
  
  <p>I also tried the code below: </p>
  
  <pre><code>String strg = (String)js.executeScript("alert("+elemID+");").toString();
  </code></pre>
  
  <p>This prints <code>[object]</code> too (just a note: using <code>toString()</code> breaks the loop, not sure why)</p>
  
  <p>How can I print the value of <code>elemID</code> in each iteration?</p>
  
  <p>If I print <code>elemID</code> outside of JavaScript, it prints the value but I want it inside of JavaScript.</p>
  
  <hr>
  
  <p>EDIT:
  If I do </p>
  
  <pre><code>for (WebElement el : listOfElem){
      String elemID = el.getAttribute("id");
      System.out.println(elemID);
      Javascriptexecutor js = (Javascriptexecutor) driver;
      String strg = (String) js.executeScript("alert("+elemID+");");
      System.out.println(strg);
  }
  </code></pre>
  
  <p>It prints value for the 1st System.out correctly but the rest of the iterations prints <code>null</code></p>
  
  <p>If I use below as suggested in comments</p>
  
  <p><code>String strg = (String) js.executeScript("console.log("+elemID+");");</code> in the above edited code</p>
  
  <p>It prints the value (1st Sys.out) and thats it, after that it breaks the loop and comes out. </p>