WebDriver API 实例详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebDriver API 实例详解相关的知识,希望对你有一定的参考价值。

一、访问某网页地址

被测试网页的网址:

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   @Test
12   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 block
18         e.printStackTrace();
19     }
20   }
21   @BeforeMethod
22   public void beforeMethod() {
23       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
24         driver = new ChromeDriver();
25   }
26 
27   @AfterMethod
28   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   @Test
12   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 block
18         e.printStackTrace();
19     }
20   }
21   @BeforeMethod
22   public void beforeMethod() {
23       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
24         driver = new ChromeDriver();
25   }
26 
27   @AfterMethod
28   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   @Test
14   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 block
22         e.printStackTrace();
23     }
24   }
25   @BeforeMethod
26   public void beforeMethod() {
27       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
28         driver = new ChromeDriver();
29   }
30 
31   @AfterMethod
32   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   @Test
14   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 block
23         e.printStackTrace();
24     }
25   }
26   @BeforeMethod
27   public void beforeMethod() {
28       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
29         driver = new ChromeDriver();
30   }
31 
32   @AfterMethod
33   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   @Test
13   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 block
20         e.printStackTrace();
21     }
22   }
23   @BeforeMethod
24   public void beforeMethod() {
25       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
26         driver = new ChromeDriver();
27   }
28 
29   @AfterMethod
30   public void afterMethod() {
31       driver.quit();
32   }
33 
34 }
View Code

 

以上是关于WebDriver API 实例详解的主要内容,如果未能解决你的问题,请参考以下文章

WebDriver高级应用实例

Selenium JavascriptExecutor 详解

Selenium JavascriptExecutor 详解

WebDriver高级应用实例(10)

python+selenium——详解介绍Selenium常用API的使用--python语言(完整版)

Selenium常用API详解介绍