判断页面元素是否存在
测试网址代码
<html>
<head>
<title>你喜欢的水果</title>
</head>
<body>
<input id='button' type='button' onclick="alert('这是一个alert弹窗');" value='单击此按钮,弹出alert弹出窗'/></input>
</body>
</html>
Java语言版本API实例
@Test
public void testHandleAlert() {
driver.manage().window().maximize();
driver.get(url);
WebElement button = driver.findElement(By.xpath("//input"));
button.click();
try {
//使用driver.switchTo.alert()方法获取Alert对象
Alert alert = driver.switchTo().alert();
//断言判断
Assert.assertEquals("这是一个alert弹窗", alert.getText());
alert.accept();
} catch (NoAlertPresentException exception) {
Assert.fail("尝试操作的alert框未被找到");
exception.printStackTrace();
}
}