4

I want to check if an element exists on the page(which shouldnt) and continue if it doesnt.

driver.wait(function() {   
        fieldBool = driver.isElementPresent(webdriver.By.id("someId"));
        return fieldBool;
}, timeout).then(function(b) {
        assert.equal(false, b, "message");    
});

I want fieldBool to be false, but the test stops(timeout or no such element exists).

2
  • I found this example: expect(element(by.css('#logout')).isPresent()).toBeTruthy();. You can review the item here: stackoverflow.com/questions/21245418/… Commented Nov 25, 2014 at 18:26
  • 1
    So i should use toBeFalsy() ? Commented Nov 25, 2014 at 18:35

3 Answers 3

5

The WebDriver will throw an error when an element does not exist. So, we have to check for the NoSuchElementError.

We can check that an element does not exist by making use of the optional callbacks for the .findElement promise driver.findElement(...).then(successCallback,errorCallback) which will get called as the parameter names imply.

The following is how I've been doing it:

var existed = await driver.findElement(webdriver.By.id("someId")).then(function() {
    return true;//it was found
}, function(err) {
    if (err instanceof webdriver.error.NoSuchElementError) {
        return false;//element did not exist
    } else {
        webdriver.promise.rejected(err);//some other error...
    }
});
assert.equal(existed,false,"message");
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know about all the magix in the webdriver.promise world, but man is it difficult to find out how to see if element exists! .then(success, fail)! that's all! THANK YOU ARTHUR WEBORG
Happy to be of assistance, really glad to hear my digging helped someone else out! (2 days on and off haha) agreed on the difficulty >_< too bad the method from the original answer was deprecated, looked much simpler!
1
driver.findElements(webdriver.By.id('someId')).then((elements)=>{
   if(elements.length > 0){
     throw new Error('The Element was found!');
   } else {
     return true;
   }
});

This works by finding all of the elements that match the id, css, name etc, and making sure that the result that comes back (which would be an array of elements) has a length of 0 - meaning no elements were found with that css/id/name etc on the page.

Comments

0

Try this,

driver.isElementPresent(webdriver.By.id("someId")).then(
    function(isPresent){
        assert.equal(false, isPresent , "message");   
    }
)

I hope it helps you

2 Comments

.isElementPresent(...) doesn't appear to be a method according to the nodejs spec for the webDriver. is driver here from the standard npm package (selenium-webdriver)?
.isElementPresent is deprecated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.