本篇知识点:
- 操作Frame中的页面元素
- 使用Frame中的HTML源码内容来操作Frame
- 操作IFrame中的页面元素
- 操作浏览器的Cookie
操作Frame中的页面元素
方法:driver.switchTo().frame("frame的id");
被测试网页的html代码:
frameset.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>frameset 页面</title> </head> <frameset cols="25%,50%,25%"> <frame id="leftframe" src="frame_left.html" /> <frame id="middleframe" src="frame_middle.html" /> <frame id="rightframe" src="frame_right.html" /> </frameset> </html>frame_left.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>左侧 frame</title> </head> <body> <p>这是左侧 frame 页面上的文字</p> </body> </html>
frame_middle.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>中间 frame</title> </head> <body> <p>这是中间 frame 页面上的文字</p> </body> </html>
frame_right.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>右侧 frame</title> </head> <body> <p>这是右侧 frame 页面上的文字</p> </body> </html>
实例代码:
// 操作Frame中的页面元素 @Test public void testHandleFrame() { driver.get("file:///F:/workspace/WebDriver%20API/frameset.html"); // 使用driver.switchTo().frame方法切换到想要的坐厕frame页面,如果没有使用慈航代码,无法找到leftframe里的元素 driver.switchTo().frame("leftframe"); WebElement leftFrameText = driver.findElement(By.xpath("//p")); // 添加断言,左侧frame中的文字是否和"这是左侧 frame 页面上的文字"这几个关键字相一致 Assert.assertEquals("这是左侧 frame 页面上的文字", leftFrameText.getText()); // 使用driver.switchTo().parentFrame(),从左侧frame中返回到frameset页面 // 如果不调用此代码,无法从左侧frame页面中直接进入其他frame页面 driver.switchTo().defaultContent(); // 切换到中间的frame driver.switchTo().frame("middleframe"); WebElement middleFrameText = driver.findElement(By.xpath("//p")); Assert.assertEquals("这是中间 frame 页面上的文字", middleFrameText.getText()); driver.switchTo().defaultContent(); // 切换到右侧的frame driver.switchTo().frame("rightframe"); WebElement rightFrameText = driver.findElement(By.xpath("//p")); Assert.assertEquals("这是右侧 frame 页面上的文字", rightFrameText.getText()); driver.switchTo().defaultContent(); // 除了用frame的名称来切换到另一个frame,可以用索引号来进入。从0开始 driver.switchTo().frame(1); middleFrameText = driver.findElement(By.xpath("//p")); Assert.assertEquals("这是中间 frame 页面上的文字", middleFrameText.getText()); }
使用Frame中的HTML源码内容来操作Frame
能够使用Frame页面的HTML源代码定位指定的Frame页面并进行操作。
被测试网页的HTML代码同上。
实例代码:
// 使用Frame中的HTML源码内容来操作Frame @Test public void testHandleFrameByPageSource() { driver.get("file:///F:/workspace/WebDriver%20API/frameset.html"); // 找到页面上所有的frame对象,并存储到名为frames的容器中 List<WebElement> frames = driver.findElements(By.tagName("frame")); for (WebElement frame : frames) { // 切换到目标frame中 driver.switchTo().frame(frame); // 如果当前进入的frame中源码包含关键字,则查找该页面中p标签的对象 if (driver.getPageSource().contains("中间 frame")) { WebElement middleFrameText = driver.findElement(By.xpath("//p")); Assert.assertEquals("这是中间 frame 页面上的文字", middleFrameText); break;// 已经找到目标frame,退出循环 } else {// 如果不包含关键字,则返回frameset页面中 driver.switchTo().defaultContent(); } } driver.switchTo().defaultContent(); }
操作IFrame中的页面元素
方法:先查找出iframe元素以WebEelement保存,再调用driver.switchTo().frame(WebEelement);被测试页面的HTML代码如上,修改frame_left.html代码。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>左侧 frame</title> </head> <body> <p>这是左侧 frame 页面上的文字</p> <iframe src="iframe.html" style="width: 200px; height: 500px"></iframe> </body> </html>
新增iframe.html页面
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>iframe</title> </head> <body> <p>这是iframe页面上的文字</p> </body> </html>
实例代码如下:
// 操作IFrame中的页面元素 @Test(enabled = false) public void testHandleIFrame() { driver.get("file:///F:/workspace/WebDriver%20API/frameset.html"); //转换操作区域,进入左侧frame的页面区域 driver.switchTo().frame("leftframe"); //查找页面包含“iframe”关键字的页面元素对象 WebElement iframe=driver.findElement(By.tagName("iframe")); //转换操作区域,进入iframe的页面区域 driver.switchTo().frame(iframe); //在iframe页面区域查找p标签的页面元素 WebElement p=driver.findElement(By.xpath("//p")); //断言iframe页面中的p标签中的文字是否和“这是iframe页面上的文字”关键字像一只。 Assert.assertEquals("这是iframe页面上的文字", p.getText()); //转换操作区域,进入frameset页面区域,为进去其他frame页面区域做好准备。 driver.switchTo().defaultContent(); //转换擦欧洲哦区域,进入中间frame的页面区域 driver.switchTo().frame("middleframe"); }
操作浏览器的Cookie
操作目的:能够遍历输出所有Cookie的key和value,能够删除指定的Cookie对象,能够删除所有的Cookie对象。
被测试页面:http://www.baidu.com
实例代码:
//操作浏览器的Cookie @Test(enabled = false) public void testCookie(){ driver.get("http://www.baidu.com"); //得到当前页面下的所有cookies,并输出所在 域,name,value,有效日期和路径 Set<Cookie> cookies=driver.manage().getCookies(); Cookie newcookie=new Cookie("cookieNmae", "cookieValue"); System.out.println(String.format("Domain->name->value->expiry->path")); for(Cookie cookie:cookies){ System.out.println(String.format("%s->%s->%s->%s->%s", cookie.getDomain(),cookie.getName(),cookie.getValue(),cookie.getExpiry(),cookie.getPath())); } //删除cookie的三种方法 //通过name来删除 driver.manage().deleteCookieNamed("CookieName"); //通过cookie对象 driver.manage().deleteCookie(newcookie); //全部删除 driver.manage().deleteAllCookies(); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }