Selenium2(WebDriver)总结(五)---元素操作进阶(常用类)
1、Alert类
- Alert是指windows弹窗的一些操作,需要new一个Alert类
- driver.switchTo().alert():切换到alert窗口
- alert.getText():取得弹窗上面的字符串
- alert.accept():点击确定/ok类的按钮,使弹窗消失
- alert.dismiss():取消
public void testAlert(){
WebElement element = driver.findElement(By.className("alert"));
element.click();
Alert alert = driver.switchTo().alert();
String text = alert.getText();
alert.accept();
//alert.dismiss();
System.out.println(text);
}
使用Actions类
- 先要new一个Actions的类对象
- 最后的perform()一定要加上,否则执行不成功
public void testAlertByActions(){
WebElement element = driver.findElement(By.className("alert"));
Actions action = new Actions(driver);
action.click(element).perform();
Alert alert = driver.switchTo().alert();
String text = alert.getText();
alert.accept();
//alert.dismiss();
System.out.println(text);
}
2、Action类
- 先要new一个Actions的类对象
- 最后的perform()一定要加上,否则执行不成功
public void testActions(){
WebElement element = driver.findElement(By.className("over"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
String text = driver.findElement(By.id("over")).getText();
System.out.println(text);
}
3、调用JS
- 一般用来执行一段JS,来改变HTML
- 一些非标准控件无法用selenium2的API时,可以执行JS的办法来取代
- executeScript这个方法的参数为字符串,为一段JS代码
public void testJS(){
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("alert('helloworld')");
}
4、Wait机制及实现
- 在规定的时间内只要符合条件即返回,下面的代码中是只要isDisplayed即返回
public void testWait(){
WebElement waitButton = driver.findElement(By.id("wait"));
waitButton.click();
boolean flag = new WebDriverWait(driver, 10).until
(
new ExpectedCondition<Boolean>()
{
public Boolean apply(WebDriver driver)
{
return driver.findElement(By.className("red")).isDisplayed();
}
}
);
if(flag){
String text = driver.findElement(By.className("red")).getText();
System.out.println(text);
}
}
5、Iframe操作
- 如果iframe标签有能够唯一确定的id或者name,就可以直接用id或者name的值:driver.switchTo().frame("aa");
- 如果iframe标签没有id或者name,但能够通过页面上确定其是第几个(也就是通过index来定位iframe,index是从0开始的):driver.switchTo().frame(0);
- 还可以通过xpath的方式来定位iframe,写法如下:
- WebElement iframe = driver.findElement(By.xpath("//iframe[@name='aa']"));
- driver.switchTo().frame(iframe);
public void testIFrame(){
driver.findElement(By.id("user")).sendKeys("test");
driver.switchTo().frame("aa");
driver.findElement(By.id("user")).sendKeys("iframe test");
driver.switchTo().defaultContent();//返回顶层frame
driver.findElement(By.id("user")).sendKeys("---new test");
}
6、多窗口切换
- gettWindowHandles:取得driver所打开的所有的页面的句柄
- witchTo是指切换到相应的窗口中去,window中的参数是指要切过去的窗口的句柄
public void testMultiWindow(){
driver.findElement(By.id("user")).sendKeys("test");
String handle = driver.getWindowHandle();//获取当前窗口的句柄
System.out.println(handle);
WebElement element = driver.findElement(By.className("open"));
element.click();
Set<String> handles = driver.getWindowHandles();
for(String s : handles)
{
if(!s.equals(handle))
{
System.out.println(s);
driver.switchTo().window(s);
driver.findElement(By.id("kw")).sendKeys("glen");
}
}
driver.switchTo().window(handle);
driver.findElement(By.id("user")).sendKeys("---new test");
}
Selenium2(WebDriver)总结(五)---元素操作进阶(常用类)的更多相关文章
- JavaScript常用操作,常用类
算术运算符 重点关注 算数,赋值,逻辑运算符,三目运算符 <!DOCTYPE html> <html> <head> <meta charset=" ...
- java的eclipse操作和常用类Object的使用
1.eclipse的快捷键: (1)alt + / 内容辅助. 如:main+alt + / 会出现完整的main方法. syso+alt+ / 会输出. 如编写某个方法时,只需写入方法名 + a ...
- Selenium2(WebDriver)总结(四)---基本元素操作
WebDriver提供了常用的WEB控件的操作方法,比如:按钮.输入框.超链接等,废话不多说,直接上代码: import org.openqa.selenium.By; import org.open ...
- Selenium2(WebDriver)总结(三)---元素定位方法
元素定位的重要性不言而喻,如果定位不到元素谈何操作元素呢,webdrvier提供了很多种元素定位方法,如ID,Name,xpath,css,tagname等. 例如需要定位如下元素: <inpu ...
- Java第五天,API常用类,静态(static)、集合(ArrayList)、日期(Date)、日历(Calendar)的使用方法
上文中我们学习到了Random随机数类和ArrayList<E>集合.这两个知识点都是经常用到的,那么除了这两个外,还有哪些知识点是我们所必须掌握的呢? static 使用static需要 ...
- selenium + python自动化测试unittest框架学习(三)webdriver元素操作(二)
上一篇是元素的定位,那么定位元素的目的就是对元素进行操作,例如写入文本,点击按钮,拖动等等的操作 (1)简单元素操作 简单元素操作 find_element_by_id("kw") ...
- selenium - 常用元素操作
# 3.常用元素操作 # 元素对象的获取ele = driver.find_element_by_XXX('定位表达式') # 获取元素的文本内容(返回值为元素的文本)ele.text # 获取元素的 ...
- selenium中元素操作之浏览器窗口滚动&网页日期控件操作(js操作)(五)
js的滚动条scrollIntoView() Arguments[] - python与js之间的羁绊 1.移动到元素element对象的“底端”,与当前窗口的“底部”对齐: driver.execu ...
- Selenium webdriver 元素操作
本来这些东西网上一搜一大堆,但是本着收集的精神,整理一份放着吧!哈!哈!哈! 1. 输入框(text field or textarea) WebElement element = driver.fi ...
随机推荐
- Delphi 完全时尚手册之 Visual Style 篇 (界面不错) 转自http://blog.csdn.net/iseekcode/article/details/4733229
这里先说说两个概念:Theme(主题)和 Visual Style .Theme 最早出现在 Microsoft Plus! for Windows 95 中,是 Windows 中 Wallpape ...
- mvn常用插件目标
由于Maven在使用时非常简单,比如下面是百度百科中对Maven常用命令的列表: mvn archetype:create 创建Maven项目 mvn compile 编译源代码 mvn deploy ...
- Tomcat 负载均衡 及Session共享
原文:https://www.sunjianhua.cn/archives/tomcat-high-availability.html 一.安装java环境 二.安装tomcat(apache-tom ...
- Warning: The Copy Bundle Resources build phase contains this target's Info.plist file 'Info
WARNING: The Copy Bundle Resources build phase contains this target's Info.plist file 'Info.plist'. ...
- 在Oracle电子商务套件版本12.2中创建自定义应用程序(文档ID 1577707.1)
在本文档中 本笔记介绍了在Oracle电子商务套件版本12.2中创建自定义应用程序所需的基本步骤.如果您要创建新表单,报告等,则需要自定义应用程序.它们允许您将自定义编写的文件与Oracle电子商务套 ...
- Java锁的设计
1.自旋锁 自旋锁是采用让当前线程不停地的在循环体内执行实现的,当循环的条件被其他线程改变时 才能进入临界区.如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public ...
- jquery 实现table的行列选中效果改进
行列都可以多选,也可对相应数据进行统计: 行选中效果 列选中效果
- 在SQLite中使用事务
使用SQLiteDatabase的beginTransaction()方法可以开启一个事务,程序执行到endTransaction() 方法时会检查事务的标志是否为成功,如果为成功则提交事务,否则回滚 ...
- js混淆加密,通过混淆Js代码让别人(很难)无法还原
js混淆加密,通过混淆Js代码让别人(很难)无法还原 使用js的混淆加密,其目的是为了保护我们的前端代码逻辑,对应一些搞技术吃饭的公司来说,为了防止被竞争对手抓取或使用自己的代码,就会考虑如何加密 ...
- 第三方IDC性能测评主要指标
弹性计算性能弹性计费模式就是 "即用即付 ",最小单位可以按小时来计算.随着云计算负载的增长,企业购买服务器带宽时的资源. 1.弹性计算性能 弹性计费模式就是"即 ...