Issue
HTML Code :
<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>
Selenium Code :
String message =driver.findElement(By.className("messageStackError")).getText();
I am getting the run time error in Selenium webdriver
Unable to locate element: .messageStackError (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 19 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Solution
Actually this is the timing issue, when you're going to find it would not be present at that time on DOM
, so you should try using WebDriverWait
to wait until this element could be present as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("messageStackError")));
el.getText()
Note :- Make sure before finding the element that it is not inside any frame
or iframe
. If it is inside then you need to switch that frame
or iframe
before finding element as driver.switchTo().frame("frame id or name");
Answered By - Saurabh Gaur
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.