Rafael C.
Rafael C.

Reputation: 2345

If element is not shown with isDisplayed(), I'm getting error

My scenario

  1. access a specific page
  2. While showing the element, should I click it
  3. If don't show this element, ignore

My code

exports.checkButton = function (driver) {

    driver.findElement(By.css(".btn.btn-danger.pull-right.remove-promotion")).then(function(button){ 

        if (button.isDisplayed()) {

            console.log("Displaying"); 

    } else { 

            console.log("not displayed");

    }
});

My problem

If the element is not displayed, it does not show the message console.log("not displayed"); and I'm getting error:

Uncaught NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":".btn.btn-danger.pull-right.remove-promotion"}

Upvotes: 1

Views: 2376

Answers (3)

Linh Nguyen
Linh Nguyen

Reputation: 1138

isDisplayed() can be used only when the element exists in the DOM, but be hidden (for example, contains style="display: none"). I think, in your case, the element doesn't exist in the DOM when it is not displayed, that's why you get the exception NoSuchElementError.

Please try:

export.checkButton = function (driver) {    
        driver.findElement(By.css(".btn.btn-danger.pull-right.remove-promotion")).then(function(button){ 
            console.log("Displaying");
            button.click();
            return true;
        }).catch(function (ex) {
            console.log("not displayed");
            return false;
        });
}

var display = checkButton(driver);
while (display) {
    display = checkButton(driver);
}

Note: You should check the DOM first to see how it behaves in both cases - when the element exists and not exists.

Upvotes: 2

JeffC
JeffC

Reputation: 25597

Using .isDisplayed() assumes that the element exists. You are getting that error because the element doesn't exist. Either your locator is off or the element maybe hasn't loaded yet, etc. Use .findElements() (plural) and make sure the size is > 0 to see if the element exists. Ideally you would wrap that in a function and use it when needed. Once you determine the element exists, then check .isDisplayed() and the code should work.

Upvotes: 0

Zoette
Zoette

Reputation: 1281

If you want to make sure an element is displayed, you first need to be sure it actually exists in the DOM. In Java you can check it like this:

public boolean isElementPresentInDom(By by) {
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
    boolean isElementPresent = (driver.findElements(by).size() != 0);
    driver.manage().timeouts().implicitlyWait(driverTimeOut, TimeUnit.MILLISECONDS); // I have a driverTimeOut variable, I set the default timeout to zero in the beginning of this method to make it fast, then I set it to my driverTimeOut again. I recommend you to do the same :)
    return isElementPresent;
}

(Don't know how to translate it exactly in Javascript, but you've got the main idea.) If this method returns true, then you can check if your element is displayed.

Upvotes: 0

Related Questions