Selenium: How to assert a WebElement that doesn’t exist

Just in case you need to assert that a WebElement doesn’t exist, here’s a code snippet that you can use.

Java

public int isElementPresent(String xpath) {
    return  driver.findElements(By.xpath(xpath)).size();
}

Cucumber
@Then("^I expect to see the link On the page$")
public void iExpectToSeeTheLinkOnThePage() {
    assertTrue(demoPage.isElementPresent() > 1);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.