Java+Selenium3方法篇26-处理Alert弹窗
来源:互联网 发布:dp901单片机说明书 编辑:程序博客网 时间:2024/06/05 09:46
前面几篇文章介绍了switchTo方法,这篇继续介绍switchTo中关于处理alert弹窗的问题。很多时候,我们进入一个网站,就会弹窗一个alert框,有些我们直接关闭,有些可能有取消和确定按钮,还有些调查的alert框,可以运行用户输入文字,例如预定一个网站的资讯,输入邮箱地址就可以,每天接收一封该网站推送的邮件。以下我找到了一个alert例子,只有点击确定这个按钮,alert框才会消失。
关于处理alert的脚本代码如下:
package lessons;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;public class ElementOpration {public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("http://news.cyol.com/node_60799.htm"); Thread.sleep(2000); // 处理alert框 System.out.println(driver.switchTo().alert().getText()); driver.switchTo().alert().accept(); } }关于alert还有其他几个方法,请看下图
第一个accept()方法就是我们上面的示例,dismiss()就是点击alert框上面的取消按钮,getText()就是获取alert内部的文字,例如上面打印效果。sendKeys方法就像文章开头提到的输入邮件预定内容一样,上面还有两个关于证书的,先不用管,暂时没用过。
阅读全文
2 0
- Java+Selenium3方法篇26-处理Alert弹窗
- Java+Selenium3方法篇32-处理不安全连接
- Java+Selenium3方法篇34-如何处理日历控件
- Java+Selenium3方法篇37-Robot类处理文件上传
- Java+Selenium3方法篇38-AutoIt工具处理文件上传
- Java+Selenium3方法篇25-isDisplayed方法
- Java+Selenium3方法篇13-findElements举例
- Java+Selenium3方法篇39-Explicit wait
- Java+Selenium3方法篇41-FluentWait
- Java+Selenium3方法篇21-webdriver处理浏览器多窗口切换
- Java+Selenium3方法篇22-利用Javascript处理竖向滚动条
- Java+Selenium3方法篇27-利用JS处理元素高亮显示
- Java+Selenium3方法篇21-webdriver处理浏览器多窗口切换
- Java+Selenium3方法篇2-Timeout接口相关方法介绍
- Java+Selenium3方法篇3-window接口相关方法介绍
- Java+Selenium3方法篇30-Selenium中截图方法-TakeScreenshot
- Java+Selenium3方法篇4-Navigation接口介绍
- Java+Selenium3方法篇5-findElement之By id
- 拷贝构造函数为什么不能用值传递
- iOS 【一篇文章引发的思考 —— 异步/同步/并发/串行】
- Qt 根据Label大小无形变的自适应居中显示图像
- mybatis-config.xml:报SAXParseException: The content of element type "configuration" must match
- 分治法--快速排序
- Java+Selenium3方法篇26-处理Alert弹窗
- containsKey方法——判断是否包含指定的键名
- [搬萝卜 & 取水果]
- 解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variabl
- OkHttp简单功能封装
- 算法分析课每周练习 Merge k Sorted Lists
- JSP四种属性范围
- RocketMQ命令整理
- jQuery中对数组的值进行去重处理