WebDriver拾级而上·之十九 常用方法
一、判断元素是否存在
public static boolean isElementPresent(WebDriver driver, By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
二、判断Alert是否存在
public static boolean isAlertPresent(WebDriver driver) {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
三、获取弹框的文本
acceptNextAlert弹框的操作类型,true点击确认操作,false点击取消操作
public static String closeAlertAndGetItsText(WebDriver driver,boolean acceptNextAlert) {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}