十一、双击某个元素

被测试网页的html源码:

 <html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="text" id="inputBox"
ondblclick="javascript:this.style.background='red'">请双击</input>
</body>
</html>

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;

 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file3.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement inputBox = driver.findElement(By.id("inputBox"));
//声明Action对象
Actions builder = new Actions(driver);
//双击
builder.doubleClick(inputBox).build().perform();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

十二、操作单选下拉列表

被测试网页的html源码:

 <html>
<head>
<meta charset="UTF-8">
</head>
<body>
<select name="fruit" size="1">
<option id="peach" value="taozi">桃子</option>
<option id="watermelon" value="xigua">西瓜</option>
<option id="orange" value="juzi">橘子</option>
<option id="kiwifruit" value="mihoutao">猕猴桃</option>
<option id="maybush" value="shanzha">山楂</option>
<option id="litchi" value="lizhi">荔枝</option>
</select>
</body>
</html>

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;

 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file4.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement fruit = driver.findElement(By.name("fruit"));
Select droplist = new Select(fruit);
//根据Index,下标从0开始
droplist.selectByIndex(3);
Assert.assertEquals("猕猴桃", droplist.getFirstSelectedOption().getText());
//根据value属性值
droplist.selectByValue("shanzha");
Assert.assertEquals("山楂", droplist.getFirstSelectedOption().getText());
//通过显示的文字
droplist.selectByVisibleText("荔枝");
Assert.assertEquals("荔枝", droplist.getFirstSelectedOption().getText());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

十三、检查单选列表的选项文字是否符合期望

被测试网页的html源码:

 <html>
<head>
<meta charset="UTF-8">
</head>
<body>
<select name="fruit" size="1">
<option id="peach" value="taozi">桃子</option>
<option id="watermelon" value="xigua">西瓜</option>
<option id="orange" value="juzi">橘子</option>
<option id="kiwifruit" value="mihoutao">猕猴桃</option>
<option id="maybush" value="shanzha">山楂</option>
<option id="litchi" value="lizhi">荔枝</option>
</select>
</body>
</html>

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file4.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement fruit = driver.findElement(By.name("fruit"));
Select droplist = new Select(fruit);
//
List<String> exp_options = Arrays.asList((new String[]{"桃子","西瓜","橘子","猕猴桃","山楂","荔枝"}));
List<String> act_option = new ArrayList<String>();
for(WebElement option:droplist.getOptions()){
act_option.add(option.getText());
}
//断言
Assert.assertEquals(exp_options.toArray(), act_option.toArray());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

十四、操作多选的选择列表

被测试网页的html源码:

 <html>
<head>
<meta charset="UTF-8">
</head>
<body>
<select name="fruit" size="6" multiple=true>
<option id="peach" value="taozi">桃子</option>
<option id="watermelon" value="xigua">西瓜</option>
<option id="orange" value="juzi">橘子</option>
<option id="kiwifruit" value="mihoutao">猕猴桃</option>
<option id="maybush" value="shanzha">山楂</option>
<option id="litchi" value="lizhi">荔枝</option>
</select>
</body>
</html>

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;

 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file5.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement fruit = driver.findElement(By.name("fruit"));
Select droplist = new Select(fruit);
//选择
droplist.selectByIndex(3);
droplist.selectByValue("shanzha");
droplist.selectByVisibleText("桃子");
droplist.deselectAll();//取消全部选择 //再次选择
droplist.selectByIndex(3);
droplist.selectByValue("shanzha");
droplist.selectByVisibleText("桃子"); //逐个取消
droplist.deselectByIndex(3);
droplist.deselectByValue("shanzha");
droplist.deselectByVisibleText("桃子"); try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

 十五、操作单选框

被测试网页的html源码:

 <html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form>
<input type="radio" name="fruit" value="berry">草莓</input>
<br/>
<input type="radio" name="fruit" value="watermelon">西瓜</input>
<br/>
<input type="radio" name="fruit" value="orange">橙子</input>
</form>
</body>
</html>

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;
import java.util.List; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file6.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement radioOption = driver.findElement(By.xpath("//input[@value='berry']"));
if(!radioOption.isSelected()){
radioOption.click();
}
Assert.assertTrue(radioOption.isSelected());
//
List<WebElement> fruits = driver.findElements(By.name("fruit"));
for(WebElement fruit:fruits){
if(fruit.getAttribute("value").equals("watermelon")){
if(!fruit.isSelected()){
fruit.click();
}
Assert.assertTrue(fruit.isSelected());
break;
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

十六、操作复选框

被测试网页的html源码:

 <html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form>
<input type="checkbox" name="fruit" value="berry">草莓</input>
<br/>
<input type="checkbox" name="fruit" value="watermelon">西瓜</input>
<br/>
<input type="checkbox" name="fruit" value="orange">橙子</input>
</form>
</body>
</html>

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;
import java.util.List; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file7.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement orangcheckbox = driver.findElement(By.xpath("//input[@value='orange']"));
if(!orangcheckbox.isSelected()){
orangcheckbox.click();
}
Assert.assertTrue(orangcheckbox.isSelected());
//
List<WebElement> checkboxs = driver.findElements(By.name("fruit"));
for(WebElement checkbox:checkboxs){
checkbox.click();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

十七、检查页面元素的文本内容是否出现

被测试网页的HTML代码:

 <html>
<head>
<meta charset="UTF-8">
</head>
<body>
<p>《三生三世十里桃花》这个电影真的很棒!</p>
<p>主要是杨洋不错!</p>
</body>
</html>

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;

 import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file8.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement text = driver.findElement(By.xpath("//p[1]"));
String contentText = text.getText();
Assert.assertEquals("《三生三世十里桃花》这个电影真的很棒!", contentText);
Assert.assertTrue(contentText.contains("三生三世"));
Assert.assertTrue(contentText.startsWith("《三"));
Assert.assertTrue(contentText.endsWith("很棒!"));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

十八、执行javaScript脚本

被测试网页的网址:

http://baidu.com

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.baidu.com";
@Test
public void opentest() {
driver.get(url);
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript("return document.title");
Assert.assertEquals("百度一下,你就知道", title); try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String searchText = (String) js.executeScript("var button= document.getElementById('su');return button.value");
System.out.println(searchText);
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

十九、拖曳页面元素

被测试网页的网址:

http://jqueryui.com/resources/demos/draggable/scroll.html

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://jqueryui.com/resources/demos/draggable/scroll.html"; @Test
public void opentest() {
driver.get(url);
WebElement draggable = driver.findElement(By.id("draggable"));
//向下拖动10个像素、共拖动5次
for(int i=0;i<5;i++){
new Actions(driver).dragAndDropBy(draggable, 0, 10).build().perform();
}
//向右拖动10个像素、共拖动5次
for(int i=0;i<5;i++){
new Actions(driver).dragAndDropBy(draggable, 10, 0).build().perform();;
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

二十、模拟键盘的操作

被测试网页的网址:

http://www.baidu.com

Java语言版本的API实例代码:

 package test;

 import org.testng.annotations.Test;

 import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.baidu.com"; @Test
public void opentest() {
driver.get(url);
Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL);//按下Ctrl键
action.keyDown(Keys.SHIFT);//按下Shift键
action.keyDown(Keys.ALT);//按下Alt键
action.keyUp(Keys.CONTROL);//释放Ctrl键
action.keyUp(Keys.SHIFT);//释放Shift键
action.keyUp(Keys.ALT);//释放ALT键
//模拟键盘在搜索输入框输入大写的字符“ABCD”
action.keyDown(Keys.SHIFT).sendKeys("abcd").perform();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }

WebDriver API 实例详解(二)的更多相关文章

  1. WebDriver API 实例详解(四)

    三十一.使用页面的文字内容识别和处理新弹出的浏览器窗口 被测试网页的HTML源码: <html> <head> <meta charset="UTF-8&quo ...

  2. WebDriver API 实例详解(三)

    二十一.模拟鼠标右键事件 被测试网页的网址: http://www.sogou.com Java语言版本的API实例代码: package test; import org.testng.annota ...

  3. WebDriver API 实例详解(一)

    一.访问某网页地址 被测试网页的网址: http://www.baidu.com Java语言版本的API实例代码: 方法1: package test; import org.testng.anno ...

  4. JavaScript学习笔记-实例详解-类(二)

    实例详解-类(二)   //===给Object.prototype添加只读\不可枚举\不可配置的属性objectId(function(){ Object.defineProperty(Object ...

  5. 免费的HTML5连载来了《HTML5网页开发实例详解》连载(二)

    最近新浪.百度.腾讯.京东.大众点评.淘宝等流行的网站都加大了招聘HTML5的力度,HTML5开发人员成了抢手货,本次连载的是由大众点评前端工程师和一淘网前端工程师基情奉献的<HTML5网页开发 ...

  6. Entity Framework实例详解

    Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...

  7. 【eclipse插件开发实战】 Eclipse插件开发6——eclipse在线翻译插件Translator开发实例详解

    Eclipse插件开发6--eclipse在线翻译插件Translator开发实例详解 在上一篇文章中讲到了一个简单的eclipse插件开发实例,主要是对插件工程的基本创建步骤进行了讲解,这篇文章当中 ...

  8. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

  9. Cocos2d-x 3.X手游开发实例详解

    Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...

随机推荐

  1. 我的javascript心跳机

    li { list-style: none!important; padding:0; } .list_num{ list-style-type:decimal; } .list_inline{ ma ...

  2. 工欲善其事 之 Web 前端调试工具格式化混淆过的 JS 代码

    工欲善其事 之 Web 前端调试工具格式化混淆过的 JS 代码 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&q ...

  3. 嵌入式Linux下Qt的中文显示

    一般情况下,嵌入式Qt界面需要中文显示,下面总结自己在项目中用到的可行的办法 1,下载一种中文简体字体,比如我用的是”方正准圆简体“,把字体文件放在ARM开发板系统的Qt字库中,即/usr/lib/f ...

  4. dz数据结构

    pre_common_admincp_cmenu 后台 首页 | 常用操作管理数据表 字段名 数据类型 默认值 允许非空 自动递增 备注 id smallint(6) unsigned    NO 是 ...

  5. PyQt4工具栏

    工具栏 菜单对程序中的所有命令进行分组防治,而工具栏则提供了快速执行最常用命令的方法. #!/usr/bin/python # -*- coding:utf-8 -*- import sys from ...

  6. Mysql数据库一个表字段中存了id,并以逗号分隔,id对应的详细信息在另一个表中

    有两张表, 一张为爱好表b表 一张为用户表 u表 u表 id   名称   爱好Id 1    张三     1,2,3,4 2    李四      2,5 b表 id  名称 1    打乒乓 2 ...

  7. PHP MySQL Insert Into

    INSERT INTO 语句用于向数据库表中插入新记录. 向数据库表插入数据 INSERT INTO 语句用于向数据库表添加新记录. 语法 INSERT INTO table_name VALUES ...

  8. java框架---->quartz整合spring(一)

    今天我们学习一下quartz的定时器的使用.年轻时我们放弃,以为那只是一段感情,后来才知道,那其实是一生. quartz的简单实例 测试的项目结构如下: 一.pom.xml中定义quartz的依赖 & ...

  9. fedora/centos7防火墙FirewallD详解

    1 使用 FirewallD 构建动态防火墙 1.1 “守护进程” 1.2 静态防火墙(system-config-firewall/lokkit) 1.3 使用 iptables 和 ip6tabl ...

  10. \r\n和\n的区别

    写Java代码的时候习惯用\r\n换行,这样可移植性比较好但是,在UVa - 160中就出现了错误,来看看是为什么吧. http://bbs.csdn.net/topics/220033879