用于检查页面中是否存在元素的函数 [英] function to check whether an element is present in the page
问题描述
创建了一个函数来检查页面上是否存在元素.意图是等待指定的时间段,然后返回 false(如果不存在).
Created a function to check whether an element is present on a page or not. Intention is to wait for a specified period of time and then return false, if not present.
public boolean isElementPresent(final WebElement element) {
public boolean isElementPresent(final WebElement element) {
Wait<WebDriver> wait = new WebDriverWait(driver, 60);
return wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver webDriver) {
return element.isDisplayed() != false;
}
});
}
但是在找不到元素的情况下会抛出异常
But this is throwing exception in case of element not found
推荐答案
抛出异常是因为 findElement 如果没有找到元素就会抛出异常.它用于 isDisplayed() 方法.您可以首先检查页面上是否存在元素,然后检查它是否显示.使用以下进行第一次检查.
It is throwing exception because findElement will throw exception if no element is found. It is used in isDisplayed() method. You can 1st check, whether element is present on page then check whether it is displayed. Use following to make 1st check.
driver.findElements(byLocator).size>0
这篇关于用于检查页面中是否存在元素的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!