WebDriver API 实例详解(一)
一、访问某网页地址
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
方法1:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
driver.get("http://www.baidu.com");
try {
Thread.sleep(5000);
} 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();
} }
方法2:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
driver.navigate().to("http://www.baidu.com");
try {
Thread.sleep(5000);
} 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.hao123.com
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.baidu.com";
String url2 = "http://www.hao123.com";
@Test
public void opentest() {
driver.navigate().to(url);
driver.navigate().to(url2);
driver.navigate().back();//返回
try {
Thread.sleep(5000);
} 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.hao123.com
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.baidu.com";
String url2 = "http://www.hao123.com";
@Test
public void opentest() {
driver.navigate().to(url);
driver.navigate().to(url2);
driver.navigate().back();//返回
driver.navigate().forward();//前进
try {
Thread.sleep(5000);
} 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.hao123.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url2 = "http://www.hao123.com";
@Test
public void opentest() {
driver.navigate().to(url2);
driver.navigate().refresh();//刷新
try {
Thread.sleep(5000);
} 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.hao123.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url2 = "http://www.hao123.com";
@Test
public void opentest() {
//离屏幕左上角的位置
Point point = new Point(150, 150);
//浏览器窗口的长、宽
Dimension dimension = new Dimension(500, 500);
driver.manage().window().setPosition(point);
driver.manage().window().setSize(dimension);
driver.navigate().to(url2);
//窗口最大化
//driver.manage().window().maximize();
try {
Thread.sleep(5000);
} 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();
} }
六、获取页面的Title属性
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url2 = "http://www.baidu.com";
@Test
public void opentest() {
driver.navigate().to(url2);
String title = driver.getTitle();
System.out.println(title);
try {
Thread.sleep(5000);
} 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();
} }
七、获取当前页面的URL地址
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url2 = "http://www.baidu.com";
@Test
public void opentest() {
driver.navigate().to(url2);
String currenturl = driver.getCurrentUrl();
System.out.println(currenturl);
try {
Thread.sleep(5000);
} 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>
<input type="text" id="text" value="默认内容">文本框</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.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/" + "file.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement input = driver.findElement(By.id("text"));
//清除文本框中默认的内容
input.clear();
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>
<input type="text" id="text" value="默认内容">文本框</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.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/" + "file.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement input = driver.findElement(By.id("text"));
//清除文本框中默认的内容
input.clear();
//输入内容
input.sendKeys("输入指定内容");
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>
<input type="text" id="text" value="默认内容">文本框</input>
<input type="button" id="button" value="改变文本框的文字"
onClick=document.getElementById("text").value="改变了!"></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.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/" + "file2.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement button = driver.findElement(By.id("button"));
//点击
button.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();
} }
WebDriver API 实例详解(一)的更多相关文章
- WebDriver API 实例详解(四)
三十一.使用页面的文字内容识别和处理新弹出的浏览器窗口 被测试网页的HTML源码: <html> <head> <meta charset="UTF-8&quo ...
- WebDriver API 实例详解(三)
二十一.模拟鼠标右键事件 被测试网页的网址: http://www.sogou.com Java语言版本的API实例代码: package test; import org.testng.annota ...
- WebDriver API 实例详解(二)
十一.双击某个元素 被测试网页的html源码: <html> <head> <meta charset="UTF-8"> </head&g ...
- Cocos2d-x 3.X手游开发实例详解
Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...
- Entity Framework实例详解
Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...
- 【python3+request】python3+requests接口自动化测试框架实例详解教程
转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...
- Vue 实例详解与生命周期
Vue 实例详解与生命周期 Vue 的实例是 Vue 框架的入口,其实也就是前端的 ViewModel,它包含了页面中的业务逻辑处理.数据模型等,当然它也有自己的一系列的生命周期的事件钩子,辅助我们进 ...
- python+requests接口自动化测试框架实例详解
python+requests接口自动化测试框架实例详解 转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实 ...
- 【eclipse插件开发实战】 Eclipse插件开发6——eclipse在线翻译插件Translator开发实例详解
Eclipse插件开发6--eclipse在线翻译插件Translator开发实例详解 在上一篇文章中讲到了一个简单的eclipse插件开发实例,主要是对插件工程的基本创建步骤进行了讲解,这篇文章当中 ...
随机推荐
- 【matlab】输出显示函数 sprintf()&disp()
disp()功能类似于c语言中的print:java语言中的System.out.println(): Matlab的disp()函数 : 1.输出字符串: >>disp('my tes ...
- HBase 1.3(NOSQL) 发布,性能大幅提升
Apache HBase 1.3.0版在2017年1月中旬正式发布了,新版本支持分层数据的压缩和多个方面的性能提升,像预写日志(WAL).一个新的RPC机制,等等.HBase 1.3.0一共修 ...
- 在程序中使用命令行的方式来调用py文件
做这个主要是程序可以做到直接调用一个脚本,而不是从脚本中把类或者函数import出来这样调用,比如我们写的python命令行文件,让java来调用,让c++来调用,都是可以的.这样不需要整个语言都用p ...
- 【SSH进阶之路】Hibernate映射——一对一双向关联映射(六)
上篇博文[SSH进阶之路]Hibernate映射--一对一单向关联映射(五),我们介绍了一对一的单向关联映射,单向是指仅仅能从人(Person)这端载入身份证端(IdCard),可是反过来.不能从身份 ...
- Maven------电脑安装Maven
转载: http://blog.csdn.net/jiuqiyuliang/article/details/45390313 注意:修改本地仓库路径时,<localRepository>D ...
- Java精选笔记_EL表达式
EL表达式 初始EL EL是一种可以简化JSP页面的表达式,EL表达式的语法非常简单都是以"${"符号开始,以"}"符号结束的 EL表达式是一种简单的数据&qu ...
- Thinkphp 修改U方法按路由规则生成url
tp开户路由后,使用U方法是不会按路由规则生成url的,一般我们是要手动修改模版,把里面的U方法去掉,手动修改链接,如果是已经写好的程序,后期才添加路由,修改起链接就太麻烦了 今天无聊就修改了一下U方 ...
- List转换为数组Array的方法
List<String> list = new ArrayList<String>(); list.add("str1"); list.add(" ...
- OracleServiceORCL这个服务竟然不见了
OracleServiceORCL这个服务竟然不见了,后数据库连接不成功,晕死,以前使用数据库还能看到,现在竟然不见了?Why?我猜测原因有二: ①:电脑已经装了Oracle数据库后又装了MySql数 ...
- Change Base
Given an integer m in base B (2 ≤ B ≤ 10) (m contains no more than 1000 digits), find the value of t ...