1

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"}
Rafael C.
  • 2,245
  • 4
  • 29
  • 45
  • Check out this question http://stackoverflow.com/questions/20148857/check-if-element-exists-selenium-javascript-node-js – Aldana Dec 11 '16 at 21:27

3 Answers3

2

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.

Linh Nguyen
  • 1,120
  • 7
  • 13
  • in fact I need use this catch with while. For example, while the button is present, run `console.log("Displaying")`. Do you have any example for this? Because I'm trying here but no success – Rafael C. Dec 12 '16 at 20:55
  • So you use my code above with no success? What did it show? Any error? Did you check whether your css is correct? – Linh Nguyen Dec 12 '16 at 21:16
  • @LihnNguyen Your code works perfectly, but I need to use with `while`and `catch`to perform many times the block code while `button` is displayed. Example? – Rafael C. Dec 12 '16 at 21:20
  • I updated the code with a `click()` if the element exists. I don't understand why you need a `while`? Maybe you can update your scenario? – Linh Nguyen Dec 12 '16 at 22:17
  • because is a page with several entries, so while these records are being displayed, I need to remove them and, when it does not exist any more, I running catch :) – Rafael C. Dec 12 '16 at 22:22
  • Are they displayed at the same time, or when you remove 1, another might appear? – Linh Nguyen Dec 12 '16 at 22:25
  • Second option :) When I remove one, another can appears – Rafael C. Dec 12 '16 at 22:26
  • Please update your question with your new scenario then. I will update my answer according to it – Linh Nguyen Dec 12 '16 at 22:27
  • Please check if the code is close to what you wanted? – Linh Nguyen Dec 12 '16 at 22:37
0

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.

Zoette
  • 1,241
  • 2
  • 18
  • 49
0

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.

JeffC
  • 22,180
  • 5
  • 32
  • 55