In Selenium 2 I want to ensure that an element on the page that the driver has loaded does not exist. I'm including my naive implementation here.
WebElement deleteLink = null; try { deleteLink = driver.findElement(By.className("commentEdit")); } catch (NoSuchElementException e) { } assertTrue(deleteLink != null);
Is there a more elegant way that basically verifies to assert that NoSuchElementException was thrown?
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.
Below is my code:WebElement deleteLink = null; try { deleteLink = driver. findElement(By. className("commentEdit")); } catch (NoSuchElementException e) { } assertTrue(deleteLink != null);
Below is the syntax which is used for checking that an element with text is either invisible or not present on the DOM. WebDriverWait wait = new WebDriverWait(driver, waitTime); wait. until(ExpectedConditions. invisibilityOfElementWithText(by, strText));
If you are testing using junit and that is the only thing you are testing you could make the test expect an exception using
@Test (expected=NoSuchElementException.class) public void someTest() { driver.findElement(By.className("commentEdit")); }
Or you could use the findElements
method that returns an list of elements or an empty list if none are found (does not throw NoSuchElementException
):
... List<WebElement> deleteLinks = driver.findElements(By.className("commentEdit")); assertTrue(deleteLinks.isEmpty()); ...
or
.... assertTrue(driver.findElements(By.className("commentEdit")).isEmpty()); ....
You can use this:
Boolean exist = driver.findElements(By.whatever(whatever)).size() == 0;
If it doesn't exist will return true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With