I am getting NoSuchElementException when I check for isDisplayed() equals false for an element. Here is my code:
XYZpage.java:
public WebElement cancelButton() { return driver.findElement(By.cssSelector(".cancelButton")); }
XYZtest.java:
softAssertions.assertThat(sellerVehicleEditPage.cancelButton().isDisplayed())
.isEqualTo(false);
I am asserting here that an element is not displayed. I have seen other solutions on this website and other places where people are suggesting to try ExpectedConditions, among other things. But since the element doesn't exist even ExpectedConditions is eventually throwing NoSuchElementException. This is what I tried in XYZpage.java:
WebDriverWait wait = new WebDriverWait(driver, 5);
public WebElement cancelButton() { return wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".cancelButton"))); }
I have more than 1000 elements with each element used multiple times in different tests. So I do not want to pass By every time I need to call the element, as some of the solutions suggests. Is there a more elegant way to check conditions like .isDisplayed() equals false without getting NoSuchElementException?