一、访问某网页地址

被测试网页的网址:

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 实例详解(一)的更多相关文章

  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 实例详解(二)

    十一.双击某个元素 被测试网页的html源码: <html> <head> <meta charset="UTF-8"> </head&g ...

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

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

  5. Entity Framework实例详解

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

  6. 【python3+request】python3+requests接口自动化测试框架实例详解教程

    转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...

  7. Vue 实例详解与生命周期

    Vue 实例详解与生命周期 Vue 的实例是 Vue 框架的入口,其实也就是前端的 ViewModel,它包含了页面中的业务逻辑处理.数据模型等,当然它也有自己的一系列的生命周期的事件钩子,辅助我们进 ...

  8. python+requests接口自动化测试框架实例详解

    python+requests接口自动化测试框架实例详解   转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实 ...

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

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

随机推荐

  1. 非常不错的前端框架Kendo-ui

    近期公司在做重构,准备换前端框架由Extjs换kendo-ui,问什么要换框架呢?主要有以下几个原因: Extjs太重,偏向后端语言,前端写起来费劲 Extjs执行太慢(这是主要原因),因为Extjs ...

  2. Animy.js,自己编写的功能丰富的html动画库

    近期由于项目须要.本人制作了一款js的动画插件.能够用于完毕各种js动画.比方移动.拉升.变色等等,全部动画经常使用的功能都已经实现,比方播放.暂停.停止.循环.加速.减速.反向播放.缓动.路径动画. ...

  3. MySQL<数据库的高级操作>

    数据库的高级操作 MySQL提供了一个mysqldump命令,它可以实现数据的备份 数据的备份 1.备份单个数据库 mysqldump -uusername -ppassword dbname [tb ...

  4. POJ 1661 Help Jimmy(递推DP)

    思路: 1. 每个板子有左右两端, dp[i][0], dp[i][1] 分别记录左右端到地面的时间 2. 从下到上递推计算, 上一层的板子必然会落到下面的某一层板子上, 或者地面上 总结: 1. 计 ...

  5. Linux alias 命令

    alias命令用于查看或设置命令别名,但仅作用于该次登陆的会话,若要永久使用别名,可在 ~/.bashrc 中设定别名 [root@localhost ~]$ alias // 查看别名 [root@ ...

  6. Activity、Window和View三者间的关系有一定的见解

    一.简述如何将Activity展现在手机上 Tips: Activity本身是没办法处理显示什么控件(view)的,是通过PhoneWindow进行显示的 换句话说:activity就是在造Phone ...

  7. Sublime Text 快捷键使用

    Sublime Text 2包含了大量快捷操作,而且还很方便修改和追加自己喜欢的快捷键.查看快捷键的方式也很简单:------------------------------------------- ...

  8. java后端接收前端传来的复杂对象(包含List对象集合)

    最近在和安卓对接口的时候发现往java后端传数据的时候,后台对象无法接收. 说明:后台对象为 类似结构 ObjectA{ private String  a; private String b; pr ...

  9. 以用户名注册来分析三种Action获取数据的方式

    1.注入属性 直接注入属性: public String userName; public String getUserName() { return userName; } public void ...

  10. Egret置于后台时,暂停游戏逻辑 (Egret 5 )

    官网教程-生命周期:http://developer.egret.com/cn/2d/lifecycle 主要是在游戏置于后台时,关闭游戏逻辑.渲染逻辑和背景音乐,保证更好的用户体验. 一 Egret ...