采用下面的例子来编写用例

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>frame switch</title>
<link
href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"
rel="stylesheet" />
</head>
<body>
<div class="row-fluid">
<div class="span10 well">
<h3>frame</h3>
<iframe id="if" name="nf" src="http://www.baidu.com" width="800" height="300"></iframe>
</div>
</div>
</body>
<script
src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script>
</html>

代码:

package com.test.frame;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions; public class frameSwitch { public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/frame/frame.html");
driver.manage().window().maximize(); // 利用id来切换
waitTime(2000);
driver.switchTo().frame("if"); waitTime(2000);
if (driver.findElement(By.xpath("//*[@id='kw']")).isDisplayed()) {
driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("switch successfully");
// 切换到父frame
driver.switchTo().parentFrame();
try {
driver.findElement(By.xpath("//*[@id='kw']"));
} catch (NoSuchElementException e) {
String js = "alert(\"switch to parent\")";
((JavascriptExecutor) driver).executeScript(js);
waitTime(3000);
driver.switchTo().alert().dismiss();
}
} // 利用name来切换
waitTime(2000);
driver.switchTo().frame("nf"); waitTime(2000);
if (driver.findElement(By.xpath("//*[@id='kw']")).isDisplayed()) {
driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("switch successfully");
driver.switchTo().parentFrame();
try {
driver.findElement(By.xpath("//*[@id='kw']"));
} catch (NoSuchElementException e) {
String js = "alert(\"switch to parent\")";
((JavascriptExecutor) driver).executeScript(js);
waitTime(3000);
driver.switchTo().alert().dismiss();
}
} // 利用定位元素来切换-xpath
waitTime(2000);
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='if']"))); waitTime(2000);
if (driver.findElement(By.xpath("//*[@id='kw']")).isDisplayed()) {
driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("switch successfully");
driver.switchTo().parentFrame();
try {
driver.findElement(By.xpath("//*[@id='kw']"));
} catch (NoSuchElementException e) {
String js = "alert(\"switch to parent\")";
((JavascriptExecutor) driver).executeScript(js);
waitTime(3000);
driver.switchTo().alert().dismiss();
}
} // 利用定位元素来切换-css
waitTime(2000);
driver.switchTo().frame(driver.findElement(By.cssSelector("#if"))); waitTime(2000);
if (driver.findElement(By.xpath("//*[@id='kw']")).isDisplayed()) {
driver.findElement(By.xpath("//*[@id='kw']")).sendKeys("switch successfully");
driver.switchTo().parentFrame();
try {
driver.findElement(By.xpath("//*[@id='kw']"));
} catch (NoSuchElementException e) {
String js = "alert(\"switch to parent\")";
((JavascriptExecutor) driver).executeScript(js);
waitTime(3000);
driver.switchTo().alert().dismiss();
driver.quit();
}
}
} static public void waitTime(int time) { try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

selenium测试(Java)--多表单切换(十二)的更多相关文章

  1. selenium基础(多表单切换、多窗口切换)

    一.多表单的切换 frame:HTML页面中的一中框架,主要作用是在当前页面中指定区域显示另一页面元素:              (HTML语言中,frame/iframe标签为表单框架) 在web ...

  2. Selenium基础知识(四)表单切换

    在测试过程中,经常会碰到frame和iframe,嵌套等情况 这种情况下直接通过id,name等等是无法定位到的 好在selenium替我们想到了这个问题switch_to方法解决问题 switch_ ...

  3. Selenium 3----定位一组元素+多表单切换+多窗口切换

    定位一组元素 和定位单个元素类似,WebDriver提供了8种用于定位一组元素的方法.定位一组元素的方法与定位单个元素的方法类似,唯一的区别是在单词element后面多了一个s表示复数. find_e ...

  4. selenium 多表单切换处理(iframe/frame)

    在web应用中,前台网页的设计一般会用到iframe/frame表单嵌套页面的应用.简单的就是一个页面签嵌套多个HEML/JSP文件.selenium webdriver  只能在同一页面识别定位元素 ...

  5. selenium webdriver——多表单切换与多窗口切换

    多表单切换 >>在Web应用中经常会遇到frame/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于frame/iframe 表单内嵌页面上的元素无 ...

  6. Selenium 2自动化测试实战15(多表单切换)

    一.多表单切换 在web应用中经常会遇到frame/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于frame/iframe表单内嵌页面上的元素无法直接定位.这 ...

  7. selenium多表单切换以及多窗口切换、警告窗处理

    selenium表单切换 在做UI自动化,有时候要定位的元素属性在页面上明明是唯一的.却怎么也不执行对元素的操作动作,这时候多半是iframe表单在作怪. 切入表单:iddriver.switch_t ...

  8. java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例

    java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...

  9. java工具类(二)之java正则表达式表单验证

    java正则表达式表单验证类工具类(验证邮箱.手机号码.qq号码等) 这篇文章主要介绍了java使用正则表达式进行表单验证工具类,可以验证邮箱.手机号码.qq号码等方法,需要的朋友可以参考下. jav ...

  10. webdriver--定位一组元素+iframe表单切换

    定位一组元素:find_elements,返回的是list,所以可以用列表的索引对列表里的某个元素操作,也可以用for循环访问list,依次操作各元素 driver.find_elements_by_ ...

随机推荐

  1. bootstrap 多元素共用 popover

    <div class="popover fade bottom in small" role="tooltip" id="gPopover&qu ...

  2. 使用 powerdesigner 将数据库表结构逆向工程生成对应的word文档

    本机系统win10 + mysql 5.7.17 + powerDesigner 16.5 + mysql-connector-odbc-5.3.9-winx32.msi 1 使用 PowerDesi ...

  3. Spring的@Required注解

    该@Required注解适用于bean属性setter方法,并表示受影响的bean属性必须在XML配置文件在配置时进行填充.否则,容器会抛出一个BeanInitializationException异 ...

  4. php教程之php空白页的原因及解决方法

    php中出现空白页的原因及解决方法汇总. 很多程序员在进行php开发的时候都遇到过出现空白页的请,综合分析来说,在php编程中出现空白页面可能是由以下几个原因造成的: 1.逻辑错误逻辑错误是最难排除的 ...

  5. php 读取功能分割大文件实例详解

    在php中,对于文件的读取时,最快捷的方式莫过于使用一些诸如file.file_get_contents之类的函数.但当所操作的文件是一个比较大的文件时,这些函数可能就显的力不从心, 下面将从一个需求 ...

  6. Remoting异步回调,向在线用户广播消息

    本文目的:向Remoting在线客户端广播消息. 使用的主要技术:异步,回调,广播. 实现过程: 定义远程实例 using System; using System.Collections.Gener ...

  7. iOS登录单例

    iOS登录单例 一,工程图. 二,代码. UserInfo.h #import <Foundation/Foundation.h> @interface UserInfo : NSObje ...

  8. Linux作为路由器(一)

    前言:Linux主机可以作为路由器使用,利用路由转发功能实现不同网络内的主机能够相互通信,利用iptables的SNAT功能来实现企业内网主机访问互联网,下面做个小的实验. 实验环境:VM (1)路由 ...

  9. IOS 使用XIB 自定义View

    一般自定义View       代码方式 有 在初始化的时候添加 子Views - (id)initWithFrame:(CGRect)frame { self = [super initWithFr ...

  10. iOS真机调试出现Development cannot be enabled while your device is locked.

    手机升级到iOS 10之后,运行真机出现了Development cannot be enabled while your device is locked. 这里是你对这台电脑设置了不信任: 解决方 ...