0

I have a method isElementDisplayed with element.isDisplayed call inside.

Why does the isDisplayed return No element found when it could return a boolean?

isElementDisplayed(element: ElementFinder) {
    return element.isDisplayed();
}
Filipe Freire
  • 823
  • 7
  • 21
Jacob
  • 29
  • 4
  • 2
    Behind the scenes, selenium is first trying to find the element. Only after it finds it, it tells whether the element is displayed. When it looked to see whether the element existed, it didn't, and it threw the exception. It's a lookup priority. Yes, theoretically, selenium could return a boolean, but consider the fact that it is giving you more information than you requested! – Ron Norris Mar 23 '18 at 12:07
  • Colud You tell me how to write in typescript "if element not visible return boolean" ? – Jacob Mar 23 '18 at 12:23
  • I'm not familiar with the language, but would suggest a try/catch mechanism. In the `try` block, do exactly what you have written above. Under the `catch` block check for the exception to see that it is 'No element found', and if that is the case, return `false`. – Ron Norris Mar 23 '18 at 12:32
  • i can't get error in catch block – Jacob Mar 26 '18 at 10:39

3 Answers3

1

isDisplayed() would check if an element is visible or not, but you need to check whether an element is present in DOM or not, use isElementPresent() or isPresent():

expect(browser.isElementPresent(element(by.id('ELEMENT_ID_HERE')))).toBe(false);
expect(element(by.id('ELEMENT_ID_HERE')).isPresent()).toBe(false);

See also:

Ali Azam
  • 2,047
  • 1
  • 16
  • 25
0

exception was thrown by find element. Use try & catch block to return false.

try{
return element.isDisplayed();
} 
catch (Exception e) 
{
return false; 
// e.getMessage(); // you can console the exception message if required
}
Thareek
  • 11
  • 2
  • If try get error I will see element not found.... catch block doesn't execute... (TypeScript) – Jacob Mar 26 '18 at 10:43
0

You should try global method and call it where you need. Something like this :

public static boolean ElementVisible(By element, WebDriver driver) {

        // Maximum wait time is 60 seconds
        int maxAttempts = 2;
        long sleepTimeInMillisForEachIteration = 500;

        for (int counter = 0; counter <= maxAttempts; counter++) {
            try {

                    driver.findElement(element);        

                return true;
            } catch (NoSuchElementException | ElementNotVisibleException e) {
                if (counter == maxAttempts) {
                    return false;
                }
                try {
                    Thread.sleep(sleepTimeInMillisForEachIteration);
                    continue;
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return false;
    }

So here it will interact with element if it is visible or display otherwise it will handle exception and return false.

Helping Hands
  • 5,292
  • 9
  • 60
  • 127