Best way to check that element is not present using Selenium WebDriver with java

Roman Iuvshin picture Roman Iuvshin · Sep 4, 2012 · Viewed 124.4k times · Source

Im trying the code below but it seems it does not work... Can someone show me the best way to do this?

public void verifyThatCommentDeleted(final String text) throws Exception {
    new WebDriverWait(driver, 5).until(new ExpectedCondition<Boolean>() {
            @Override
            public Boolean apply(WebDriver input) {
                try {
                    input.findElement(By.xpath(String.format(
                            Locators.CHECK_TEXT_IN_FIRST_STATUS_BOX, text)));
                    return false;
                } catch (NoSuchElementException e) {
                    return true;
                }
            }
        });
    }

Answer

Xolv.io picture Xolv.io · Jul 15, 2013

Instead of doing findElement, do findElements and check the length of the returned elements is 0. This is how I'm doing using WebdriverJS and I expect the same will work in Java