No Such Element and using isDisplayed()

2,964 views
Skip to first unread message

Robert

unread,
Jul 2, 2012, 11:33:39 AM7/2/12
to webd...@googlegroups.com
I'm attempting to use the isDisplayed() method that is part of the Selenium API.  I have a feeling that my assumptions about using this method may be incorrect.  Here is what I'm trying to do.  I have a page in which I want to determined if a particular element exists or not.  So, I have the following code

public boolean isDueDateHeaderDisplayed() {
String xpathExpression = "//table[1]/thead/tr[2]/th[3]";
return driver.findElement(By.xpath(xpathExpression)).isDisplayed();
}

However, when I try to use this method, expecting it to return false, in the cased when the element is NOT displayed, web driver returns

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//table[1]/thead/tr[2]/th[3]"}

An exception!

Am I using this [isDisplayed()] method correctly?


arun kumar

unread,
Jul 2, 2012, 1:03:11 PM7/2/12
to webd...@googlegroups.com
Hi Robert,

isDisplayed method can be executed on the webelement. in your case it has not fetched the element at all. 

so for you it is good to catch the exception 

public boolean isDueDateHeaderDisplayed() {
String xpathExpression = "//table[1]/thead/tr[2]/th[3]";
                try{

                }catch(NoSuchElementException ne){
                }
return 
}




--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/VTahCQ4TGTYJ.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

arun kumar

unread,
Jul 2, 2012, 1:04:45 PM7/2/12
to webd...@googlegroups.com
public boolean isDueDateHeaderDisplayed() {
String xpathExpression = "//table[1]/thead/tr[2]/th[3]";
                try{
                   driver.findElement(By.xpath(xpathExpression));
                   return true; 
                }catch(NoSuchElementException ne){
                }
return false;
}

Robert

unread,
Jul 2, 2012, 4:04:15 PM7/2/12
to webd...@googlegroups.com
Excellent!! Thanks for the feedback
To unsubscribe from this group, send email to webdriver+unsubscribe@googlegroups.com.

Simon Stewart

unread,
Jul 2, 2012, 6:28:42 PM7/2/12
to webd...@googlegroups.com, webd...@googlegroups.com
The exception indicates that the element isn't on the page at all. You need to catch that exception and return false if you see it. I'd still include the visibility check if the element is found, as presence on the DOM doesn't mean the element is visible to the user yet.

Simon


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/VTahCQ4TGTYJ.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.

Simon Stewart

unread,
Jul 2, 2012, 6:31:42 PM7/2/12
to Simon Stewart, webd...@googlegroups.com
Btw, if you're using the Wait and WebDriverWait interface/class combo, you should take a look at ExpectedConditions class, which has a function on it for just this sort of thing. Off the top of my head (cos I'm in a phone) you'll end up with something like:

wait.until(visibilityOfElementLocated(By.id("foo")));

Simon 


darrell

unread,
Jul 3, 2012, 10:13:46 AM7/3/12
to webdriver
This is one of the reasons I like to write my code as:

public boolean isDueDateHeaderDisplayed() {
String xpathExpression = "//table[1]/thead/tr[2]/th[3]";
WebElement we = driver.findElement(By.xpath(xpathExpression));
return we.isDisplayed();
}

If you wrote it this way you would see that it is failing on the
findElement call. You are trying to figure out what is wrong with
isDisplayed but you aren't even making it to that code.

According to your xpath you are expecting to see at least:

<table>
<thead>
<tr><th>R1C1</th><th>R1C2</th><th>R1C3</th></tr>
<tr><th>R2C1</th><th>R2C2</th><th>R2C3</th></tr>
</thead>
</table>

You might have more than two rows in the THEAD section. You probably
have a TBODY section after the THEAD section. You might have a TFOOT
sections immediately after the THEAD section. You might have more
then 3 TH tags. If you have less than 3 TH in the first row, you'll
need some COLSPAN or having different number of columns in the headers
can cause odd behaviour.

Darrell
Reply all
Reply to author
Forward
0 new messages