/** * 判断元素是否存在 * * @param driver * @param by * @return */ public static boolean isElementPresent(WebDriver driver, By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } /** * 判断Alert是否存在 * * @param driver * @return */ public static boolean isAlertPresent(WebDriver driver) { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; } } /**获取弹框的文本 * @param driver * @param acceptNextAlert,true接受 * @return */ 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; } }