Selenium webdriver 无法使用selenium webdriver从跨度id获取动态文本
我也尝试过javascriptexecutor 尝试javascriptexecutor时,我得到:org.openqa.selenium.WebDriverException:document.getElementById(…)为空 顺便说一下,Selenium IDE使用xpath查找元素并存储文本。我将Selenium IDE测试用例导出到Java/Junit4/Webdriver,然后在eclipse中运行它,并获得相同的NoSuchElementExceptionSelenium webdriver 无法使用selenium webdriver从跨度id获取动态文本,selenium-webdriver,Selenium Webdriver,我也尝试过javascriptexecutor 尝试javascriptexecutor时,我得到:org.openqa.selenium.WebDriverException:document.getElementById(…)为空 顺便说一下,Selenium IDE使用xpath查找元素并存储文本。我将Selenium IDE测试用例导出到Java/Junit4/Webdriver,然后在eclipse中运行它,并获得相同的NoSuchElementException 请帮忙。谢谢您试图定
请帮忙。谢谢您试图定位的元素位于iframe内。该iframe的id为“mailroifrm12”。因此,您需要首先调用:
wd.switchTo().frame(“mailroifrm12”)调用findElement(…)之前的代码>
如果不先切换到iframe,则无法找到iframe中的元素。id是动态的吗?请共享网页的URL或呈现的完整HTML代码谢谢您的回复。我不相信span id是动态的。它始终是span id=“mathq2”。但是当您刷新页面或打开新页面时,文本“6+4=”是动态的。url是:在Firefox、Chrome、IE和Safari浏览器中尝试了此操作,得到的结果相同:NoTouchElementException:无法定位element@user3439764-如果答案有效,请接受。
<div id="pdiv">
<table width="100%" cellspacing="0" cellpadding="0" border="0" align="left">
<tbody>
<tr>
<td class="normtxt" width="100%" colspan="2">Please answer this simple math question</td>
</tr>
<tr>
<td width="18%" align="left" style="padding:0">
<span id="mathq2" style="padding-bottom: 5px;">6 + 4 = </span>
</td>
<td align="left">
<span id="mathans2" style="display: none;">8</span>
<input id="mathuserans2" type="text" name="mathuserans2" value="" style="width: 100px;">
</td>
<input type="hidden" value="32317377" name="txtPolliD">
</tr>
</tbody>
</table>
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;}");