Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

WebElement.isDisplayed() throws NoSuchElementException when element is not found #1880

Closed
lukeis opened this issue Mar 2, 2016 · 20 comments
Closed

Comments

@lukeis
Copy link
Member

lukeis commented Mar 2, 2016

Originally reported on Google Code with ID 1880

What steps will reproduce the problem?

WebElement myElement;
myElement.isDisplayed();


What is the expected output? What do you see instead?

This code returns true if the element is found. However if the element is not found
this code DOES NOT return false. Instead NoSuchElementException is thrown.


What version of the product are you using? On what operating system?

Selenium 2.0 rc2, Java.

Please provide any additional information below.

Work-around it to wrap this in a try catch. However isDisplayed() should follow it's
contract and return a boolean always. Users will need a try-catch whenever they care
about isDisplayed returning false.

Reported by writewale on 2011-06-20 19:32:45

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

If the element is cached, StaleElementReferenceException is thrown instead.

Reported by writewale on 2011-06-20 19:36:43

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Which browser are you seeing this with?

Reported by jmleyba on 2011-06-20 20:39:02

  • Status changed: NeedsClarification
  • Labels added: Lang-Java, Component-WebDriver

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Firefox 3.6.17

Reported by writewale on 2011-06-20 20:52:12

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

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

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

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

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Exception is thrown on this line:

return list.isDisplayed();

Reported by writewale on 2011-06-22 17:57:38

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

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

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

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

  • Status changed: WorkingAsIntended

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

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

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

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

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Thanks for the clarification.

Reported by writewale on 2011-06-27 16:42:51

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

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

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

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

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

saku saku

Reported by color.extension on 2012-08-14 16:45:09

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

is there any work arround to this problem.

Thanks

Reported by jampanij on 2013-03-15 00:16:10

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Please provide a sample HTML test case and your code.

Reported by arran.huxtable on 2013-03-15 09:24:25

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

am facing the same problem.. please solve this problem

Reported by tarun.sujit on 2013-04-04 11:32:46

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

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

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

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

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Reported by luke.semerau on 2015-09-17 18:13:03

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis lukeis closed this as completed Mar 2, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant