Cannot catch NoSuchElementException with Selenide
Asked Answered
B

2

7

I’m trying to catch NoSuchElementException. This is my code:

public void checkActiveApps() {
    try {
        $(BUTTON).click();
    } catch (org.openqa.selenium.NoSuchElementException e) {
        System.out.println(e);
    }
}

But the exception is still thrown. How to catch it?

This is the log:

Element not found {button[role='checkbox']}
Expected: visible
Screenshot: file:/Users/user/source/project/build/reports/tests/1537866631954.0.png
Page source: file:/Users/user/source/project/build/reports/tests/1537866631954.0.html
Timeout: 4 s.
Caused by: NoSuchElementException: Unable to locate element: button[role='checkbox']
    at com.codeborne.selenide.impl.WebElementSource.createElementNotFoundError(WebElementSource.java:31)
    at com.codeborne.selenide.impl.ElementFinder.createElementNotFoundError(ElementFinder.java:82)
    at com.codeborne.selenide.impl.WebElementSource.checkCondition(WebElementSource.java:59)
    at com.codeborne.selenide.impl.WebElementSource.findAndAssertElementIsVisible(WebElementSource.java:72)
    at com.codeborne.selenide.commands.Click.execute(Click.java:16)
    at com.codeborne.selenide.commands.Click.execute(Click.java:12)
    at com.codeborne.selenide.commands.Commands.execute(Commands.java:144)
    at com.codeborne.selenide.impl.SelenideElementProxy.dispatchAndRetry(SelenideElementProxy.java:90)
    at com.codeborne.selenide.impl.SelenideElementProxy.invoke(SelenideElementProxy.java:65)

I use selenide version 4.12.3

Berman answered 25/9, 2018 at 9:23 Comment(0)
T
10

Selenide does not throw Selenium exceptions as it uses it's own.

You can try using:

public void checkActiveApps() {
    try {
        $(BUTTON).click();
    } catch (com.codeborne.selenide.ex.ElementNotFound e) {
        System.out.println(e);
    }
}

Why do you want to catch it anyway?

Tanyatanzania answered 19/11, 2018 at 10:36 Comment(2)
if it throws its own why cannot one see it in the terminal/console ? That's confusing!Reservation
In my use case I'd like it to try to click something for a couple of seconds and then give up and ignore it is not found. (Useful to click notification elements that may disappear by itself)Erny
P
1

also it is not possible to catch selenide exception by catching general class Exception because it extends from Error class so possible to catch with

public void checkActiveApps() {
    try {
        $(BUTTON).click();
    } catch (Error e) {
        System.out.println(e);
    }
}
Pitchford answered 23/2 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.