To check if an item is present on the page

A function has been created to check for the presence of an element on the page or not. The intention is to wait for a certain period of time and then return false if not.

public boolean isElementPresent (end element of WebElement) {

    Wait<WebDriver> wait = new WebDriverWait(driver, 60);
    return wait.until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver webDriver) {
            return element.isDisplayed() != false;
        }
    });
}

But this throws an exception if the item is not found.

+1
source share
4 answers

This is an exception exception because findElement will throw an exception if the item is not found. It is used in the isDisplayed () method. You can first check to see if an item is present on the page, and then check to see if it is displayed. Use the following command to do the first check.

driver.findElements(byLocator).size>0
+2

. : p

public boolean isElementPresent() {
    try {
        new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocatedBy(By.id(""));
        return true;  
    } catch (TimeOutException e) {
        return false;
    }
}

WebDriverWait -, TimeOut Exception . . ( ), true.

, , isDisplayed isEnabled, WebElement. , -, .

0

"" : -

public boolean isElementPresent() {  
    List<WebElement> elements = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementsLocatedBy(By.id(""));
    if (elements.isEmpty()) {
        return false;
    } else {
        return true;
    }
}
0

.

 public boolean fncCheckElement() {
try {
    WebElement element = (new WebDriverWait(driver,1)).until(ExpectedConditions.presenceOfElementLocated((By.id("ID"))));
 system.out.println("Element is present in web page")  
 return true;  
} catch (Throwable e) {
    ex.printStackTrace();
    system.out.println("Element is not present  in web page")
    return false;
}}

!

0
source

Source: https://habr.com/ru/post/1534387/


All Articles