Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;                 }             }         });     } 
like image 738
Roman Iuvshin Avatar asked Sep 04 '12 19:09

Roman Iuvshin


People also ask

How do you verify whether the element is present or not?

To make sure that an element is present you can do the following: driver. findElements(By.id("id")); That will return an array, if that array size is > 0 then the element/s is present.

How do you check if an element is not present in a page?

isDisplayed() is the method used to verify a presence of a web element within the webpage. The method returns a “true” value if the specified web element is present on the web page and a “false” value if the web element is not present on the web page.

How does Selenium handle element not displayed?

First Solution: Try to write unique XPATH that matches with a single element only. Second Solution: Use Explicit wait feature of Selenium and wait till the element is not visible. Once it is visible then you can perform your operations.


1 Answers

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

like image 164
Xolv.io Avatar answered Sep 21 '22 01:09

Xolv.io