0

I've set up a method to return a boolean depending on whether the element exists however it is not working properly. Also, I've imported org.openqa.selenium.NoSuchElementException; but it is not used

Method

public static boolean checkElement(WebDriver driver, WebElement element) {
        return element.isDisplayed()  || element.isEnabled();
    }
Fenzox
  • 334
  • 4
  • 20

4 Answers4

0

There are different ways to handle:

  1. Here it is checking through findElements, if no element is found then list size will be zero, if element found then the size will be greater than zero.
            List<WebElement> elements = driver.findElements(By.xpath(xpathLocation));
            if (elements.size() > 0) {
                LOGGER.info("{} is present on the DOM.", id);
                return true;
            } else {
                LOGGER.info("{} is not present on the DOM.", id);
                return false;
            }
  1. Here if the element is found then it'll return flag as true, if not and in case NoSuchElementException as well, it is handled and you'll get false.
    /**
     * Method to check if element is present.
     *
     * @param element
     * @return
     */
    public static boolean isElementPresent(WebElement element) {
        try {
            if (element.isDisplayed() || element.isEnabled())
                return true;
        } catch (Exception e) {
            return false;
        } 
        return false;
    }
  • If I get the element before implementing this method, wouldn't there be an error already at driver.findElement – Fenzox Jul 07 '20 at 13:04
  • Yes. I think i need to change the Exception, but this will help when Element is not visible or other cases. – Durga Prasad Behera Jul 07 '20 at 13:19
  • I tried option 2 and 1. Option 1 always returns size 0 for some reason. Option 2 when creating the webelement it throws no such element – Fenzox Jul 07 '20 at 13:27
0

Add Java Exceptions - Try...Catch:

public static boolean checkElement(WebDriver driver, WebElement element) {
    try {
        return element.isDisplayed()  || element.isEnabled();
    } catch (Exception e) {
        return false;
    }
}
frianH
  • 7,295
  • 6
  • 20
  • 45
0

There was a problem with my previous method where it requires the creation of the element before the method which will throw the error no such element exception. By adding the xpath to the method instead, it will fix the problem and end up in the catch section if it is not found.

public static boolean checkElement(WebDriver driver, String xpath) {
     boolean flag = false;
        try {
            WebElement element = driver.findElement(By.xpath(xpath));
            if (element.isDisplayed() || element.isEnabled())
                flag = true;
        } catch (NoSuchElementException e) {
            flag = false;
        } 
        return flag;
}
Fenzox
  • 334
  • 4
  • 20
0

As per Selenium's clients element exists is a undecissive state. Instead you need to probe if the WebElement is either among the following states:

-present -visible -interactable

To validate the different stages of an WebElement and the respective ExpectedConditions are as follows:

  • Presence of an element :

    presenceOfElementLocated(By locator)
    An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
    
  • Visibility of an element :

    visibilityOf(WebElement element)
    An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
    
  • Element to be Clickable :

    elementToBeClickable(By locator)
    An expectation for checking an element is visible and enabled such that you can click it.
    

Note : As per the docs Element is Clickable - it is Displayed and Enabled.


This usecase

Validating the different states of the WebElement:

  • presence:

    WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("element_xpath")));
    
  • visiblity:

    WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOf(element));
    
  • interactability:

    WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(element));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352