I just want to check if the element is present on the page or not?
I am confused with what can be used. What is feasible to use isDisplayed()
or isPresent()
?
What is the difference between these two?
isPresent
function isDisplayed
returns True
only if the element is displayed on the webpage and is actually visible. If you just want to check if the element is present then you can do one of the following:
findElement
inside a try/catch block. If it goes inside catch
with NoSuchElementException
then the element is not present. findElements
instead of findElement
and if length of the list returned by findElements
is zero, then the element is not present. to simplify.. I've posted code below
public static boolean isElementPresent(final WebDriver driver, By by) {
try {
waitForElement(driver, by, 2);
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.