Several of my tests refer to importBrokerCheckBox. But not all tests have to find it, because he must be absent. That is, if the element is on the page, then we must click on it, and if it is not there, we must go further through the code.But below code doesn't work and throws exception - xpath not found
importBrokerCheckBox = $x(".//div[text()='Брокер']/../../../preceding-sibling::span//span[@aria-label=\"caret-down\"]"),
if (importBrokerCheckBox.isDisplayed());
{
importBrokerCheckBox.click();
}
if (importBrokerSettingsCheckbox.isDisplayed());
{
importBrokerSettingsCheckbox.click();
}
CodePudding user response:
There are basically two options:
- try/catch block - if element is not found (
NoSuchElementException
occured) the catch block code is executed (can be empty ofc). - using
List<WebElement> elements = driver.findElements(By.by);
if no element is found,NoSuchElementException
is NOT thrown and the list stays empty likenew ArrayList<WebElement>()
.