[英]Selenium WebDriver: Check if element present or not for a while loop?
[英]What can be used if I want to check if the element is present or not in selenium webdriver?
我只想檢查頁面上是否存在該元素?
我對可以使用的東西感到困惑。 使用isDisplayed()
或isPresent()
可行嗎?
兩者有什么區別?
isPresent
函數 isDisplayed
才會返回True
。 如果只想檢查元素是否存在,則可以執行以下操作之一:
findElement
的代碼放在try / catch塊中。 如果這樣下去,里面catch
與NoSuchElementException
,則元素不存在。 findElements
代替findElement
如果通過返回列表的長度findElements
是零,則該元素不存在。 為了簡化..我已經在下面發布了代碼
public static boolean isElementPresent(final WebDriver driver, By by) {
try {
waitForElement(driver, by, 2);
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.