title: Unable to get dynamic text from a span id with selenium webdriver
body: <pre><code>            &lt;div id="pdiv"&gt;
              &lt;table width="100%" cellspacing="0" cellpadding="0" border="0" align="left"&gt;
              &lt;tbody&gt;
              &lt;tr&gt;
              &lt;td class="normtxt" width="100%" colspan="2"&gt;Please answer this simple math question&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
              &lt;td width="18%" align="left" style="padding:0"&gt;
              &lt;span id="mathq2" style="padding-bottom: 5px;"&gt;6 + 4 = &lt;/span&gt;
              &lt;/td&gt;
              &lt;td align="left"&gt;
              &lt;span id="mathans2" style="display: none;"&gt;8&lt;/span&gt;
              &lt;input id="mathuserans2" type="text" name="mathuserans2" value="" style="width: 100px;"&gt;
              &lt;/td&gt;
              &lt;input type="hidden" value="32317377" name="txtPolliD"&gt;
              &lt;/tr&gt;
              &lt;/tbody&gt;
              &lt;/table&gt;
  </code></pre>
  
  <p>I am trying to get dynamic span text from the above html code using webdriver but getting:</p>
  
  <p>org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"mathq2"}</p>
  
  <p>I need to get the text:  ' 6 + 4 = ' from span id 'mathq2'</p>
  
  <p>This text ' 6 + 4 = ' is chaning everytime page refreshes or new page loaded</p>
  
  <p>I have tried xpath, id, cssselector but always getting the Unable to locate element: {"method":"id","selector":"mathq2"}</p>
  
  <p>Here is my code:</p>
  
  <pre><code>  private static WebDriverWait wait = new WebDriverWait(wd,90);
  
   try{
      wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("mathq2")));
   }catch(org.openqa.selenium.TimeoutException e){
  System.out.println("wait time outexception: " + e); 
    }
  
  
    wd.findElement(By.xpath(".//*[@id='mathq2']")).click();
    mathTxt = wd.findElement(By.xpath(".//*[@id='mathq2']")).getText();
  
   //wd.findElement(By.id("mathq2")).click();
   //mathTxt = wd.findElement(By.id("mathq2")).getText();
  
  
   //mathTxt = wd.findElement(By.cssSelector("#pdiv span.mathq2")).getText();
  
   //mathTxt = (String)((JavascriptExecutor)wd).executeScript("    {document.getElementById('mathq2').innerHTML;}");
  </code></pre>
  
  <p>I have also tried javascriptexecutor 
  When tried javascriptexecutor I got:org.openqa.selenium.WebDriverException: document.getElementById(...) is null</p>
  
  <p>By the way Selenium IDE is finding the element with xpath and storing the text. I export the Selenium IDE test case to Java/Junit4/Webdriver, then run it in eclipse and getting the same NoSuchElementException.</p>
  
  <p>Please help. Thanks</p>