HTML代码:
<table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr class="messageStackError">
<td class="messageStackError">
<img src="images/icons/error.gif" border="0" alt="Error" title=" Error "/>
Error: Invalid administrator login attempt.</td>
</tr>
</table>
硒代码:
String message =driver.findElement(By.className("messageStackError")).getText();
我在Selenium Webdriver中遇到运行时错误
找不到元素:.messageStackError(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:19毫秒有关此错误的文档,请访问:http ://seleniumhq.org/exceptions/no_such_element.html
实际上,这是时间问题,当您打算在该时间点不存在该元素时DOM
,因此您应该尝试使用WebDriverWait
,直到该元素可能如下所示:-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("messageStackError")));
el.getText()
注意:-在找到元素之前,请确保它不在任何frame
或中iframe
。如果它在里面,那么您需要切换它frame
或iframe
在找到元素之前driver.switchTo().frame("frame id or name");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句