example of a wrapper method in Selenium

Modified on Tue, 28 Mar, 2023 at 4:32 PM

Here's an example of a wrapper method in Selenium that checks if an element is present on a webpage:

public boolean isElementPresent(By locator) {

    try {

        driver.findElement(locator);

        return true;

    } catch (NoSuchElementException e) {

        return false;

    }

}

This method takes a locator and returns a boolean value indicating whether or not the element is present on the webpage. It wraps around the findElement method provided by Selenium and catches the NoSuchElementException to prevent the test from failing if the element is not found.




Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article