【Selenium】12Alert

来源:互联网 发布:mysql数据库设计工具 编辑:程序博客网 时间:2024/06/14 10:54
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>
<input type="button" onclick="alert('这是Alert');"value="Alert"/>
<br/>
<input type="button" onclick="confirm('这是Confirmation');"value="Confirmation"/>
<br/>
<input type="button" onclick="prompt('这是Prompt');"value="Prompt"/>
<br/>
</body>
</html>

①Accept():点击确定按钮(NoAlertPresentException)

WebElement bnt=driver.findElement(By.xpath("//input[1]"));
bnt.click();
Thread.sleep(3000);
Alert alert=driver.switchTo().alert()
alert.getText();
alert.accept();

②dismiss():点击取消按钮

WebElement bnt=driver.findElement(By.xpath("//input[1]"));
bnt.click();
Thread.sleep(3000);
driver.switchTo().alert().dismiss();

③sendKeys():对prompt有效,输入

WebElement prompt=driver.findElement(By.xpath("//input[1]"));
bnt.click();
Thread.sleep(3000);
driver.switchTo().alert().sendKeys(“”);

 ④getText():弹出文本框文本内容

WebElement prompt=driver.findElement(By.xpath("//input[1]"));
bnt.click();
Thread.sleep(3000);
driver.switchTo().alert().getText(“”);

原创粉丝点击