Closed lukeis closed 8 years ago
If the element is cached, StaleElementReferenceException is thrown instead.
Reported by writewale
on 2011-06-20 19:36:43
Which browser are you seeing this with?
Reported by jmleyba
on 2011-06-20 20:39:02
NeedsClarification
Firefox 3.6.17
Reported by writewale
on 2011-06-20 20:52:12
Can you post your full code to reproduce this, and point out which line the exception
is being thrown on?
I'm guessing driver.findElement(...) is throwing the NoSuchElementException rather
than element.isDisplayed(), which is expected behaviour.
Reported by dawagner
on 2011-06-22 11:17:42
The code I gave is complete. I am using Page Factory Pattern from Selenium 2 so there
is no driver.findElement(...) in my code, the page factory automatically finds the
element. The code I posted will work if the id of the element is "myElement". Here
is another example that requires css to find the element:
@FindBy(css="div[class*=list_link]")
WebElement list;
public boolean isListDisplayed(){
return list.isDisplayed();
}
Reported by writewale
on 2011-06-22 17:56:19
Exception is thrown on this line:
return list.isDisplayed();
Reported by writewale
on 2011-06-22 17:57:38
To be clear, the page factory ultimately calls driver.findElement and that is the source
of the Exception, but that specific call is not done in my code. Thanks.
Reported by writewale
on 2011-06-22 21:59:16
This is intended behaviour; @FindBy is simply a convenient way of doing the element
lookup - if the element is not present, the correct behaviour is to throw, as if you'd
called findElement yourself and it had failed.
Reported by dawagner
on 2011-06-24 17:42:16
WorkingAsIntended
Unfortunatelly, the real reason for opening this defect has being missed.
WebElement.isDisplayed() is supposed to always return a boolean, but it only returns
a boolean if an element is found. If an element is NOT found, it should still return
a boolean, instead it throws an exception. Essentially WebElement.isDisplayed never
returns false.
Using FindBy or driver.findElement is irrelevant to the issue and was only mentioned
when a different code example was requested.
I disagree that this is working as intended, the method is not following standard Java
behavior.
Reported by writewale
on 2011-06-24 21:26:01
WebElement.isDisplayed() isn't itself throwing.
All of the methods on the WebElement class assume that the WebElement has been successfully
located - their semantics do not and should not depend on the fact that the PageFactory
uses magic proxies to inline element location.
.isDisplayed() exists to tell you whether the element, which has already been located,
is visible on the page; i.e. whether its width and height are greater than zero, it
isn't hidden by CSS, etc. If the element is present on the page, but has style="display:
none;" then isDisplayed() will return false.
@FindsBy slightly alters this semantic, because it combines the location inlined with
the .isDisplayed call, but if we were to special-case this to swallow the exception,
we would be hiding information that may be useful to other users.
What you want is a hybrid WebElement.isPresentAndDisplayed() method. We are unlikely
to provide one, because it would clutter the API, and we already provide the building
blocks to create one:
public static boolean isPresentAndDisplayed(final WebElement element) {
try {
return element.isDisplayed();
} catch (NoSuchElementException e) {
return false;
}
}
Reported by dawagner
on 2011-06-24 22:42:22
Thanks for the clarification.
Reported by writewale
on 2011-06-27 16:42:51
I tried with the hybrid method, but still getting the same exception NoSuchElementException
in the stack trace. Below is what I have tried
public static boolean isPresentAndDisplayed(final WebElement element) {
try {
return element.isDisplayed();
} catch (NoSuchElementException e) {
return false;
}
}
the calling condition is
if(isPresentAndDisplayed(driver.findElement(By.xpath(".//*[@id='WC_ContentContainerTop_TableCell_2']/table/tbody/tr[1]/td/table/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr/td[2]/span"))))
pls advice
Reported by surajray123
on 2012-02-20 12:53:46
I am facing the same issue.Can you please provide any alternative for this.
Reported by sayhi.gemini
on 2012-07-26 08:13:47
saku saku
Reported by color.extension
on 2012-08-14 16:45:09
is there any work arround to this problem.
Thanks
Reported by jampanij
on 2013-03-15 00:16:10
Please provide a sample HTML test case and your code.
Reported by arran.huxtable
on 2013-03-15 09:24:25
am facing the same problem.. please solve this problem
Reported by tarun.sujit
on 2013-04-04 11:32:46
I am having a similar problem. I am preparing ta selenium webdriver based test suite
for our product. I want to verify sugessions list is displayed when I type in a search
box.
When I use inspect element option in chrome I found my desired element as
<div class="ac_results" style="display:none">
In my test code I refer element as
findElement(By.className("ac_results"))).isDisplayed()); but it throws NoSuchelementException.
I even tried with @FindBy(how = How.XPATH, using="<xpath>") as well in both cases I
got the same exception.
Can anyone help me how to overcome this?
Reported by jayanathkarunarathna
on 2013-07-26 10:25:19
Hi,
I am facing the same issue. Please provide some guidance.
Even after using the hybrid method, I am getting the same exception.
Reported by vaibhav.milan
on 2015-03-02 08:11:38
Reported by luke.semerau
on 2015-09-17 18:13:03
Originally reported on Google Code with ID 1880
Reported by
writewale
on 2011-06-20 19:32:45