三十一、使用页面的文字内容识别和处理新弹出的浏览器窗口
被测试网页的HTML源码:
1 2 3 4你喜欢的水果 5 6 7你爱吃的水果么?
8 9 sogou搜索10 11
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 import java.util.Set; 9 10 import org.openqa.selenium.By;11 import org.openqa.selenium.NoSuchWindowException;12 import org.openqa.selenium.WebDriver;13 import org.openqa.selenium.WebElement;14 import org.openqa.selenium.chrome.ChromeDriver;15 import org.testng.Assert;16 import org.testng.annotations.AfterMethod;17 18 public class ChormeOpen {19 WebDriver driver;20 21 @Test22 public void opentest() {23 File file = new File("");24 String url = file.getAbsolutePath() + "/html/" + "file12.html";25 driver.get(url);26 String parentWindowHandle = driver.getWindowHandle();27 WebElement sogouLink = driver.findElement(By.xpath("//a"));28 sogouLink.click();29 SetallWindowsHandles = driver.getWindowHandles();30 if(!allWindowsHandles.isEmpty()){31 for(String windowHandle:allWindowsHandles){32 try {33 if(driver.switchTo().window(windowHandle).getPageSource().contains("搜狗搜索")){34 driver.findElement(By.id("query")).sendKeys("sogou");35 }36 } catch (NoSuchWindowException e) {37 // TODO: handle exception38 e.printStackTrace();39 }40 }41 }42 driver.switchTo().window(parentWindowHandle);43 Assert.assertEquals(driver.getTitle(), "你喜欢的水果");44 try {45 Thread.sleep(3000);46 } catch (InterruptedException e) {47 // TODO Auto-generated catch block48 e.printStackTrace();49 }50 }51 52 @BeforeMethod53 public void beforeMethod() {54 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");55 driver = new ChromeDriver();56 }57 58 @AfterMethod59 public void afterMethod() {60 driver.quit();61 }62 63 }
三十二、操作JavaScript的Alert弹窗
目的:能够模拟单击弹出的Alert窗口的-‘确定’-按钮
被测试网页的HTML源码:
1 2 3 4你喜欢的水果 5 6 7 8 9
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 import java.util.Set; 9 10 import org.openqa.selenium.Alert;11 import org.openqa.selenium.By;12 import org.openqa.selenium.NoAlertPresentException;13 import org.openqa.selenium.NoSuchWindowException;14 import org.openqa.selenium.WebDriver;15 import org.openqa.selenium.WebElement;16 import org.openqa.selenium.chrome.ChromeDriver;17 import org.testng.Assert;18 import org.testng.annotations.AfterMethod;19 20 public class ChormeOpen {21 WebDriver driver;22 23 @Test24 public void opentest() {25 File file = new File("");26 String url = file.getAbsolutePath() + "/html/" + "file13.html";27 driver.get(url);28 WebElement button = driver.findElement(By.xpath("//input"));29 button.click();30 try {31 Thread.sleep(3000);32 } catch (InterruptedException e) {33 // TODO Auto-generated catch block34 e.printStackTrace();35 }36 try {37 Alert alert = driver.switchTo().alert();38 Assert.assertEquals("这是一个alert弹出框", alert.getText());39 alert.accept();//单击确定按钮40 } catch (NoAlertPresentException e) {41 // TODO: handle exception42 Assert.fail("alert未被找到");43 e.printStackTrace();44 }45 try {46 Thread.sleep(3000);47 } catch (InterruptedException e) {48 // TODO Auto-generated catch block49 e.printStackTrace();50 }51 }52 53 @BeforeMethod54 public void beforeMethod() {55 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");56 driver = new ChromeDriver();57 }58 59 @AfterMethod60 public void afterMethod() {61 driver.quit();62 }63 64 }
三十三、操作JavaScript的confirm弹窗
目的:能够模拟单击JavaScript弹出的confirm框中的“确定”和“取消”按钮。
被测试网页的HTML源码:
1 2 3 4你喜欢的水果 5 6 7 8 9
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.Alert;10 import org.openqa.selenium.By;11 import org.openqa.selenium.NoAlertPresentException;12 import org.openqa.selenium.WebDriver;13 import org.openqa.selenium.WebElement;14 import org.openqa.selenium.chrome.ChromeDriver;15 import org.testng.Assert;16 import org.testng.annotations.AfterMethod;17 18 public class ChormeOpen {19 WebDriver driver;20 21 @Test22 public void opentest() {23 File file = new File("");24 String url = file.getAbsolutePath() + "/html/" + "file14.html";25 driver.get(url);26 WebElement button = driver.findElement(By.xpath("//input"));27 button.click();28 try {29 Thread.sleep(3000);30 } catch (InterruptedException e) {31 // TODO Auto-generated catch block32 e.printStackTrace();33 }34 try {35 Alert alert = driver.switchTo().alert();36 Assert.assertEquals("这是一个confirm弹出框", alert.getText());37 //alert.accept();//单击确定按钮38 alert.dismiss();//单击取消39 } catch (NoAlertPresentException e) {40 // TODO: handle exception41 Assert.fail("confirm未被找到");42 e.printStackTrace();43 }44 try {45 Thread.sleep(3000);46 } catch (InterruptedException e) {47 // TODO Auto-generated catch block48 e.printStackTrace();49 }50 }51 52 @BeforeMethod53 public void beforeMethod() {54 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");55 driver = new ChromeDriver();56 }57 58 @AfterMethod59 public void afterMethod() {60 driver.quit();61 }62 63 }
三十四、操作JavaScript的prompt弹窗
目的:能够在JavaScript的prompt弹窗中输入自定义的字符串,单击“确定”按钮和“取消”按钮。
被测试网页的HTML源码:
1 2 3 4你喜欢的水果 5 6 7 8 9
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.Alert;10 import org.openqa.selenium.By;11 import org.openqa.selenium.NoAlertPresentException;12 import org.openqa.selenium.WebDriver;13 import org.openqa.selenium.WebElement;14 import org.openqa.selenium.chrome.ChromeDriver;15 import org.testng.Assert;16 import org.testng.annotations.AfterMethod;17 18 public class ChormeOpen {19 WebDriver driver;20 21 @Test22 public void opentest() {23 File file = new File("");24 String url = file.getAbsolutePath() + "/html/" + "file15.html";25 driver.get(url);26 WebElement button = driver.findElement(By.xpath("//input"));27 button.click();28 try {29 Thread.sleep(3000);30 } catch (Exception e) {31 // TODO Auto-generated catch block32 e.printStackTrace();33 }34 try {35 Alert alert = driver.switchTo().alert();36 Assert.assertEquals("这是一个prompt弹出框", alert.getText());37 alert.sendKeys("想改变命运,就必须每天学习2小时");38 Thread.sleep(3000);39 alert.accept();//单击确定按钮40 //alert.dismiss();//单击取消41 } catch (NoAlertPresentException e) {42 // TODO: handle exception43 Assert.fail("confirm未被找到");44 e.printStackTrace();45 }catch (Exception e) {46 // TODO: handle exception47 e.printStackTrace();48 }49 try {50 Thread.sleep(3000);51 } catch (Exception e) {52 // TODO Auto-generated catch block53 e.printStackTrace();54 }55 }56 57 @BeforeMethod58 public void beforeMethod() {59 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");60 driver = new ChromeDriver();61 }62 63 @AfterMethod64 public void afterMethod() {65 driver.quit();66 }67 68 }
三十五、操作Frame中的页面元素
目的:能够进入页面的不同Frame中进行页面元素的操作。
被测试网页的HTML源码:
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.Assert;14 import org.testng.annotations.AfterMethod;15 16 public class ChormeOpen {17 WebDriver driver;18 19 @Test20 public void opentest() {21 File file = new File("");22 String url = file.getAbsolutePath() + "/html/" + "frameset.html";23 driver.get(url);24 driver.switchTo().frame("leftframe");25 WebElement leftframeText = driver.findElement(By.xpath("//p"));26 Assert.assertEquals("这是左侧frame页面上的文字", leftframeText.getText());27 driver.switchTo().defaultContent();28 //29 driver.switchTo().frame("middleframe");30 WebElement middleframeText = driver.findElement(By.xpath("//p"));31 Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText());32 driver.switchTo().defaultContent();33 //34 driver.switchTo().frame("rightframe");35 WebElement rightframeText = driver.findElement(By.xpath("//p"));36 Assert.assertEquals("这是右侧frame页面上的文字", rightframeText.getText());37 driver.switchTo().defaultContent();38 //39 //使用索引。从0开始40 driver.switchTo().frame(1);41 middleframeText = driver.findElement(By.xpath("//P"));42 Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText());43 44 try {45 Thread.sleep(3000);46 } catch (Exception e) {47 // TODO Auto-generated catch block48 e.printStackTrace();49 }50 }51 52 @BeforeMethod53 public void beforeMethod() {54 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");55 driver = new ChromeDriver();56 }57 58 @AfterMethod59 public void afterMethod() {60 driver.quit();61 }62 63 }
三十六、使用Frame中的HTML源码内容来操作Frame
目的:能够使用Frame页面的HTML源码定位指定的Frame页面并进行操作。
被测试网页的HTML源码:
同三十五被测试页面的HTML源码。
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 import java.util.List; 9 10 import org.openqa.selenium.By;11 import org.openqa.selenium.WebDriver;12 import org.openqa.selenium.WebElement;13 import org.openqa.selenium.chrome.ChromeDriver;14 import org.testng.Assert;15 import org.testng.annotations.AfterMethod;16 17 public class ChormeOpen {18 WebDriver driver;19 20 @Test21 public void opentest() {22 File file = new File("");23 String url = file.getAbsolutePath() + "/html/" + "frameset.html";24 driver.get(url);25 Listframes = driver.findElements(By.tagName("frame"));26 for(WebElement frame:frames){27 driver.switchTo().frame(frame);28 if(driver.getPageSource().contains("中间frame")){29 WebElement middleframeText = driver.findElement(By.xpath("//p"));30 Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText());31 break;32 }else{33 driver.switchTo().defaultContent();34 }35 }36 driver.switchTo().defaultContent();37 try {38 Thread.sleep(3000);39 } catch (Exception e) {40 // TODO Auto-generated catch block41 e.printStackTrace();42 }43 }44 45 @BeforeMethod46 public void beforeMethod() {47 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");48 driver = new ChromeDriver();49 }50 51 @AfterMethod52 public void afterMethod() {53 driver.quit();54 }55 56 }
三十七、操作IFrame中的页面元素
被测试网页的HTML源码:
同三十五被测试页面的HTML源码,只是需要更新如下页面的HTML源码:
修改frame_left.html源码:
在frame_left.html同级目录下新增iframe.html文件:
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 import org.openqa.selenium.By; 9 import org.openqa.selenium.WebDriver;10 import org.openqa.selenium.WebElement;11 import org.openqa.selenium.chrome.ChromeDriver;12 import org.testng.Assert;13 import org.testng.annotations.AfterMethod;14 15 public class ChormeOpen {16 WebDriver driver;17 18 @Test19 public void opentest() {20 File file = new File("");21 String url = file.getAbsolutePath() + "/html/" + "frameset.html";22 driver.get(url);23 driver.switchTo().frame("leftframe");24 WebElement iframe = driver.findElement(By.tagName("iframe"));25 driver.switchTo().frame(iframe);26 WebElement p = driver.findElement(By.xpath("//p"));27 Assert.assertEquals("这是iframe页面上的文字", p.getText());28 driver.switchTo().defaultContent();29 driver.switchTo().frame("middleframe");30 try {31 Thread.sleep(3000);32 } catch (Exception e) {33 // TODO Auto-generated catch block34 e.printStackTrace();35 }36 }37 38 @BeforeMethod39 public void beforeMethod() {40 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");41 driver = new ChromeDriver();42 }43 44 @AfterMethod45 public void afterMethod() {46 driver.quit();47 }48 49 }
三十八、操作浏览器的Cookie
目的:能够遍历输出所有的Cookie的Key和value;能够删除指定的Cookie对象;能够删除所有的Cookie对象。
被测试网页的地址:
http:www.sogou.com
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 5 import org.testng.annotations.BeforeMethod; 6 import java.util.Set; 7 import org.openqa.selenium.Cookie; 8 import org.openqa.selenium.WebDriver; 9 import org.openqa.selenium.chrome.ChromeDriver;10 import org.testng.annotations.AfterMethod;11 12 public class ChormeOpen {13 WebDriver driver;14 String url = "http://www.sogou.com";15 @Test16 public void opentest() {17 driver.get(url);18 Setcookies = driver.manage().getCookies();19 Cookie newCookie = new Cookie("cookieName","cookieValue");20 System.out.println(String.format("Domain-> name -> value -> expiry -> path"));21 for(Cookie cookie:cookies){22 System.out.println(String.format("%s-> %s -> %s -> %s -> %s",23 cookie.getDomain(),cookie.getName(),24 cookie.getValue(),cookie.getExpiry(),cookie.getPath()));25 }26 //删除cookie的3种方法27 //128 driver.manage().deleteCookieNamed("CookieName");29 30 //231 driver.manage().deleteCookie(newCookie);32 33 //334 driver.manage().deleteAllCookies();35 36 try {37 Thread.sleep(3000);38 } catch (Exception e) {39 // TODO Auto-generated catch block40 e.printStackTrace();41 }42 }43 44 @BeforeMethod45 public void beforeMethod() {46 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");47 driver = new ChromeDriver();48 }49 50 @AfterMethod51 public void afterMethod() {52 driver.quit();53 }54 55 }