1
public boolean addNewAlertIsDisplayed() {       
    if(driver.findElement(By.xpath("//*[@class='aside right am-slide-right']")).isDisplayed()) {
    return true;
    }
    else {
        return false;
    }
}

I am expecting false if it is not there..

But getting:

org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //*[@class='aside right am-slide-right'] (WARNING: The server did not provide any stacktrace information)

Why this exception is coming.. Is this needs to be handled in try/catch?

7
  • yes, if element is not availablein the DOM, then it will throw that exception. Need to be handled with try/catch block. Commented Jul 2, 2018 at 12:15
  • Seems you got an accepted answer but ideally you need to induce try-catch{} Commented Jul 2, 2018 at 12:39
  • @DebanjanB Best practice is to avoid throwing exceptions when unnecessary. This also follows the best practices outlined in the Selenium docs, findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead. See link. Commented Jul 2, 2018 at 12:52
  • @DebanjanB I don't understand your point. OP is using '.findElement` to validate whether the element is displayed. You are advising using .findElement() and a try-catch()... which is contrary to best practices and guidelines mentioned above. Commented Jul 2, 2018 at 14:45
  • @DebanjanB You understand that you are advocating an approach that goes against best practices and the Selenium documentation? Commented Jul 2, 2018 at 14:54

2 Answers 2

2

Please check and use any one of the below two approaches.

Approach 1: you can find the Element using findElements method.If the element is found, then elementList size will be 1 or more than 1 and If the element is not found, then size will be 0. So, we can return the flag based on the condition,

    public boolean addNewAlertIsDisplayed(){
        List<WebElement> elementList=driver.findElements(By.xpath("//*[@class='aside right am-slide-right']"));

        //If the Element is not present , then the above list size will be 0, else it will have some value
        if(elementList.size()>0){
                return true;
        }else{
            return false;
        }
    }

Approach 2: You can check the element present using the try catch block as below

    public boolean addNewAlertIsDisplayed(){
        try{
            driver.findElement(By.xpath("//*[@class='aside right am-slide-right']"));
            return true;
        }catch(NoSuchElementException e ){
                return false;
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Why are you posting the same answers?
Best practice is to avoid throwing exceptions when unnecessary. This also follows the best practices outlined in the Selenium docs, findElement should not be used to look for non-present elements, use findElements(By) and assert zero length response instead. See link.
neither of these solutions will tell you if an element is displayed... it only checks if they exist in the DOM.
1

Try this:

public boolean addNewAlertIsDisplayed() {
  List<WebElement> elements = driver.findElements(By.xpath("//*[@class='aside right am-slide-right']"));
  return elements.size() > 0 && elements.get(0).isDisplayed();
}

If there are no elements it won't throw any exception. It will just be a list of size 0. Also as @Corey Goldberg mentioned, there should be checked if this element actually displayed. I forgot to add this to method.

2 Comments

just beware that this answer doesn't actually tell you if the element is displayed as the OP asked... it only checks if it exists in the DOM. See the isDislayedI() method on WebElement to check if it's actually displayed.
@CoreyGoldberg thank you for the feedback, nice catch! I have edited my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.