博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WebDriver API 实例详解(一)
阅读量:6967 次
发布时间:2019-06-27

本文共 12389 字,大约阅读时间需要 41 分钟。

一、访问某网页地址

被测试网页的网址:

http://www.baidu.com

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

方法1:

1 package test; 2  3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8  9 public class ChormeOpen {10     WebDriver driver;11   @Test12   public void opentest() {13       driver.get("http://www.baidu.com");14       try {15         Thread.sleep(5000);16     } catch (InterruptedException e) {17         // TODO Auto-generated catch block18         e.printStackTrace();19     }20   }21   @BeforeMethod22   public void beforeMethod() {23       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");24         driver = new ChromeDriver();25   }26 27   @AfterMethod28   public void afterMethod() {29       driver.quit();30   }31 32 }
View Code

方法2:

1 package test; 2  3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8  9 public class ChormeOpen {10     WebDriver driver;11   @Test12   public void opentest() {13       driver.navigate().to("http://www.baidu.com");14       try {15         Thread.sleep(5000);16     } catch (InterruptedException e) {17         // TODO Auto-generated catch block18         e.printStackTrace();19     }20   }21   @BeforeMethod22   public void beforeMethod() {23       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");24         driver = new ChromeDriver();25   }26 27   @AfterMethod28   public void afterMethod() {29       driver.quit();30   }31 32 }
View Code

二、返回上一个访问的网页(模拟点击浏览器的后退功能)

被测试网页的网址:

http://www.hao123.com

http://www.baidu.com

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

1 package test; 2  3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8  9 public class ChormeOpen {10     WebDriver driver;11     String url = "http://www.baidu.com";12     String url2 = "http://www.hao123.com";13   @Test14   public void opentest() {15       driver.navigate().to(url);16       driver.navigate().to(url2);17       driver.navigate().back();//返回18       try {19         Thread.sleep(5000);20     } catch (InterruptedException e) {21         // TODO Auto-generated catch block22         e.printStackTrace();23     }24   }25   @BeforeMethod26   public void beforeMethod() {27       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");28         driver = new ChromeDriver();29   }30 31   @AfterMethod32   public void afterMethod() {33       driver.quit();34   }35 36 }
View Code

三、从上次访问网页前进到下一个网页(模拟单击浏览器的前进功能)

被测试网页的网址:

http://www.hao123.com

http://www.baidu.com

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

1 package test; 2  3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8  9 public class ChormeOpen {10     WebDriver driver;11     String url = "http://www.baidu.com";12     String url2 = "http://www.hao123.com";13   @Test14   public void opentest() {15       driver.navigate().to(url);16       driver.navigate().to(url2);17       driver.navigate().back();//返回18       driver.navigate().forward();//前进19       try {20         Thread.sleep(5000);21     } catch (InterruptedException e) {22         // TODO Auto-generated catch block23         e.printStackTrace();24     }25   }26   @BeforeMethod27   public void beforeMethod() {28       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");29         driver = new ChromeDriver();30   }31 32   @AfterMethod33   public void afterMethod() {34       driver.quit();35   }36 37 }
View Code

四、刷新当前网页

被测试网页的网址:

http://www.hao123.com

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

1 package test; 2  3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8  9 public class ChormeOpen {10     WebDriver driver;11     String url2 = "http://www.hao123.com";12   @Test13   public void opentest() {14       driver.navigate().to(url2);15       driver.navigate().refresh();//刷新16       try {17         Thread.sleep(5000);18     } catch (InterruptedException e) {19         // TODO Auto-generated catch block20         e.printStackTrace();21     }22   }23   @BeforeMethod24   public void beforeMethod() {25       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");26         driver = new ChromeDriver();27   }28 29   @AfterMethod30   public void afterMethod() {31       driver.quit();32   }33 34 }
View Code

 五、操作浏览器窗口

被测试网页的网址:

http://www.hao123.com

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

1 package test; 2  3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.Dimension; 6 import org.openqa.selenium.Point; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.chrome.ChromeDriver; 9 import org.testng.annotations.AfterMethod;10 11 public class ChormeOpen {12     WebDriver driver;13     String url2 = "http://www.hao123.com";14   @Test15   public void opentest() {16       //离屏幕左上角的位置17       Point point = new Point(150, 150);18       //浏览器窗口的长、宽19       Dimension dimension = new Dimension(500, 500);20       driver.manage().window().setPosition(point);21       driver.manage().window().setSize(dimension);22       driver.navigate().to(url2);23       //窗口最大化24       //driver.manage().window().maximize();25       try {26         Thread.sleep(5000);27     } catch (InterruptedException e) {28         // TODO Auto-generated catch block29         e.printStackTrace();30     }31   }32   @BeforeMethod33   public void beforeMethod() {34       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");35         driver = new ChromeDriver();36   }37 38   @AfterMethod39   public void afterMethod() {40       driver.quit();41   }42 43 }
View Code

六、获取页面的Title属性

被测试网页的网址:

http://www.baidu.com

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

1 package test; 2  3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8  9 public class ChormeOpen {10     WebDriver driver;11     String url2 = "http://www.baidu.com";12   @Test13   public void opentest() {14       driver.navigate().to(url2);15       String title = driver.getTitle();16       System.out.println(title);17       try {18         Thread.sleep(5000);19     } catch (InterruptedException e) {20         // TODO Auto-generated catch block21         e.printStackTrace();22     }23   }24   @BeforeMethod25   public void beforeMethod() {26       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");27         driver = new ChromeDriver();28   }29 30   @AfterMethod31   public void afterMethod() {32       driver.quit();33   }34 35 }
View Code

七、获取当前页面的URL地址

被测试网页的网址:

http://www.baidu.com

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

1 package test; 2  3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8  9 public class ChormeOpen {10     WebDriver driver;11     String url2 = "http://www.baidu.com";12   @Test13   public void opentest() {14       driver.navigate().to(url2);15       String currenturl = driver.getCurrentUrl();16       System.out.println(currenturl);17       try {18         Thread.sleep(5000);19     } catch (InterruptedException e) {20         // TODO Auto-generated catch block21         e.printStackTrace();22     }23   }24   @BeforeMethod25   public void beforeMethod() {26       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");27         driver = new ChromeDriver();28   }29 30   @AfterMethod31   public void afterMethod() {32       driver.quit();33   }34 35 }
View Code

八、在输入框中清除原有的文字内容

被测试网页的HTML源码:

1 2 3 
4 5 6 文本框7 8
View Code

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

1 package test; 2  3 import org.testng.annotations.Test; 4  5 import org.testng.annotations.BeforeMethod; 6  7 import java.io.File; 8  9 import org.openqa.selenium.By;10 import org.openqa.selenium.WebDriver;11 import org.openqa.selenium.WebElement;12 import org.openqa.selenium.chrome.ChromeDriver;13 import org.testng.annotations.AfterMethod;14 15 public class ChormeOpen {16     WebDriver driver;17   @Test18   public void opentest() {19       File file = new File("");20       System.out.println(file.getAbsolutePath());21       String url = file.getAbsolutePath() + "/html/" + "file.html";22       driver.get(url);23       System.out.println(driver.getCurrentUrl());24       //25       WebElement input = driver.findElement(By.id("text"));26       //清除文本框中默认的内容27       input.clear();28       try {29         Thread.sleep(3000);30     } catch (InterruptedException e) {31         // TODO Auto-generated catch block32         e.printStackTrace();33     }34   }35   @BeforeMethod36   public void beforeMethod() {37       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");38         driver = new ChromeDriver();39   }40 41   @AfterMethod42   public void afterMethod() {43       driver.quit();44   }45 46 }
View Code

九、在输入框中输入指定内容

被测试网页的HTML源码:

1 2 3 
4 5 6 文本框7 8
View Code

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

1 package test; 2  3 import org.testng.annotations.Test; 4  5 import org.testng.annotations.BeforeMethod; 6  7 import java.io.File; 8  9 import org.openqa.selenium.By;10 import org.openqa.selenium.WebDriver;11 import org.openqa.selenium.WebElement;12 import org.openqa.selenium.chrome.ChromeDriver;13 import org.testng.annotations.AfterMethod;14 15 public class ChormeOpen {16     WebDriver driver;17   @Test18   public void opentest() {19       File file = new File("");20       System.out.println(file.getAbsolutePath());21       String url = file.getAbsolutePath() + "/html/" + "file.html";22       driver.get(url);23       System.out.println(driver.getCurrentUrl());24       //25       WebElement input = driver.findElement(By.id("text"));26       //清除文本框中默认的内容27       input.clear();28       //输入内容29       input.sendKeys("输入指定内容");30       try {31         Thread.sleep(3000);32     } catch (InterruptedException e) {33         // TODO Auto-generated catch block34         e.printStackTrace();35     }36   }37   @BeforeMethod38   public void beforeMethod() {39       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");40         driver = new ChromeDriver();41   }42 43   @AfterMethod44   public void afterMethod() {45       driver.quit();46   }47 48 }
View Code

十、单击按钮

被测试网页的HTML源码:

1  2  3 
4 5 6 文本框 7 9 10
View Code

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

1 package test; 2  3 import org.testng.annotations.Test; 4  5 import org.testng.annotations.BeforeMethod; 6  7 import java.io.File; 8  9 import org.openqa.selenium.By;10 import org.openqa.selenium.WebDriver;11 import org.openqa.selenium.WebElement;12 import org.openqa.selenium.chrome.ChromeDriver;13 import org.testng.annotations.AfterMethod;14 15 public class ChormeOpen {16     WebDriver driver;17   @Test18   public void opentest() {19       File file = new File("");20       System.out.println(file.getAbsolutePath());21       String url = file.getAbsolutePath() + "/html/" + "file2.html";22       driver.get(url);23       System.out.println(driver.getCurrentUrl());24       //25       WebElement button = driver.findElement(By.id("button"));26       //点击27       button.click();28       try {29         Thread.sleep(3000);30     } catch (InterruptedException e) {31         // TODO Auto-generated catch block32         e.printStackTrace();33     }34   }35   @BeforeMethod36   public void beforeMethod() {37       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");38         driver = new ChromeDriver();39   }40 41   @AfterMethod42   public void afterMethod() {43       driver.quit();44   }45 46 }
View Code

 

转载于:https://www.cnblogs.com/successcai/p/6661500.html

你可能感兴趣的文章
TSP问题
查看>>
1、java集合:java集合详解及类关系图
查看>>
“去哪儿网”2015春招前端面试题
查看>>
Contiki Process概述
查看>>
ubuntu14.06 Lts开启ssh服务
查看>>
对象比较:Comparable 和 Comparator
查看>>
jsp中的contentType与pageEncoding的区别和作用
查看>>
swift - label 的font 设置 文字字体和大小
查看>>
git在公司内部的使用实践(转)
查看>>
普通程序员如何转向AI方向(转)
查看>>
Python是什么?
查看>>
从零开始山寨Caffe·拾:IO系统(三)
查看>>
Ubuntu下压缩解压文件
查看>>
入门指引 - PHP手册笔记
查看>>
java 调用启动远程shell脚本,启动spark
查看>>
Spring boot ----RestTemplate学习笔记
查看>>
[LUOGU] P3128 [USACO15DEC]最大流Max Flow
查看>>
windows2003server下能安装的MSN
查看>>
MyBatis和SpringMVC集成事务在Junit测试下有效但是在实际项目无效的问题
查看>>
Caffe将自己的文件生成lmdb
查看>>