Verify that an element exist in Selenium 2 or not

0 votes

In Selenium 2, I want to ensure that an element present on the page, that the driver has loaded does not exist.

Below is my code:

    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?

Aug 23, 2018 in Selenium by Martin
• 4,320 points
5,316 views

1 answer to this question.

0 votes

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 a 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());

answered Aug 23, 2018 by Meci Matt
• 9,460 points

Related Questions In Selenium

0 votes
0 answers
+3 votes
6 answers

Verifying whether an element present or visible in selenium Webdriver

Below code will help you: To check Element ...READ MORE

answered Apr 17, 2018 in Selenium by king_kenny
• 3,710 points
95,243 views
0 votes
1 answer

How do I find an element that contains specific text in Selenium WebDriver (Python)?

Try the following: driver.find_elements_by_xpath("//*[contains(text(), 'My Button')]") I hope this ...READ MORE

answered Nov 27, 2020 in Selenium by Gitika
• 65,910 points
5,778 views
0 votes
2 answers

Finding an element in Selenium

Hi, ​both find_element_by_xpath(xpath) and find_element(By.XPath, xpath) serve the ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,140 points
1,118 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
12,740 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,618 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
6,695 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,557 views
0 votes
2 answers

Check that the element is clickable or not in Selenium WebDriver

Hi , you want to know only is ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
33,345 views
0 votes
1 answer

Wait til an element appears in Selenium?

You should call ignoring with exception to ignore till ...READ MORE

answered May 24, 2018 in Selenium by Meci Matt
• 9,460 points
451 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP