十一、双击某个元素

被测试网页的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. NLP入门相关——学习笔记

    近义词.一词多义 GPT.ELMO.Bert

  2. 在VS中安装/使用 MVVMLight

    一般来说,我喜欢使用NuGet来获取这些东西,比如Newtonsoft.Json.netlog4.MVVMLight 之类的东西.至于NuGet的使用,以后再说吧.为了直接进入正题,我们这里直接使用V ...

  3. Entity Framework底层操作封装V2版本号(4)

    这个版本号里面.由于涉及到了多库的操作.原有的系统方法不能做到这种事情了.所以这里有了一点差别 这个类的主要用作就是,连接字符串的作用,默认是指向默认配置里面的,可是你能够指向其它的连接 using ...

  4. Java精选笔记_Filter(过滤器)

    Filter(过滤器) Filter入门 什么是Filter Filter被称作过滤器或者拦截器,其基本功能就是对Servlet容器调用Servlet的过程进行拦截,从而在Servlet进行响应处理前 ...

  5. Extjs不错的博客

    http://www.cnblogs.com/fangsui/category/372751.html http://www.cnblogs.com/WangJinYang/tag/EXT.NET/ ...

  6. Android中Invalidate与postInvalidate的区别<转>

    http://www.cnblogs.com/it-tomorrow/archive/2012/11/08/2760146.html 示例:http://rayleung.iteye.com/blog ...

  7. centos 安装 phalcon

    git clone --depth 1 --branch phalcon-v2.0.3 https://github.com/phalcon/cphalcon.git cd cphalcon/ext ...

  8. Js debug模式

    在代码中需要调试的地方,输入“debugger;”:

  9. 【python系列】python2.x和python3.x的区别

    刚接触python使用的是python2.x的书籍,但是发现python3.x和python2.x有不小的区别,以下做一些记录 性能 Py3.0运行 pystone benchmark的速度比Py2. ...

  10. ReactNative For Android 框架启动核心路径剖析

    版权声明:本文由王少鸣原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/144 来源:腾云阁 https://www.qclo ...