Reputation: 558
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
Upvotes: 3
Views: 27299
Reputation: 159
The element was not loaded in the DOM by the time you requested the text from it.
To delay it you can use
time.sleep(x)
# x is in seconds
in Python, this will let the element load in the DOM.
For more info you can refer to this
https://selenium-python.readthedocs.io/waits.html
Upvotes: 0
Reputation: 601
We can also solve this with the help of exception handling and infinite loop:
Here's the code in python -
while 1:
try:
driver.find_element_by_class_name("class-name")
break
except:
continue
If there is no exception then the loop will break or else if there's any exception like the element is not found, then the loop will be continued till the element is founded.
Upvotes: 0
Reputation: 23805
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");
Upvotes: 6