在web 应用中经常会出现frame 嵌套的应用,假设页面上有A、B 两个frame,其中B 在A 内,那么定位B 中的内容则需要先到A,然后再到B。switch_to_frame 方法可以把当前定位的主体切换了frame 里。怎么理解这句话呢?我们可以从frame的实质去理解。frame 中实际上是嵌入了另一个页面,而webdriver 每次只能在一个页面识别,因此才需要用switch_to_frame 方法去获取frame 中嵌入的页面,对那个页面里的元素进行定位。

下面的代码中frame.html 里有个id 为f1 的frame,而f1 中又嵌入了id 为f2 的frame,该frame 加载了百度的首页。

frame.html

html代码示例:

<html>

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>frame</title>
<script type="text/javascript" async="
"src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"rel="stylesheet" />
<script type="text/javascript">$(document).ready(function(){});</script>

</head>

<body>

<div class="row-fluid">
     <div class="span10 well">
          <h3>frame</h3>
          <iframe id="f1" src="inner.html" width="800" height="600"></iframe>
     </div>
</div>
</body>

<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>

inner.html

html代码

<html>

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>inner</title>
</head>

<body>
     <div class="row-fluid">
           <div class="span6 well">
                 <h3>inner</h3>
                 <iframe id="f2" src="http://www.baidu.com" width="700" height="400"> </iframe>
           </div>
     </div>
</body>

</html>

frame.html 中嵌套inner.html ,两个文件和我们的脚本文件放同一个目录下,通过浏览器打开,得到下列页面:

图3.8

下面通过switch_to_frame 方法来定位frame 内的元素:

代码示例:

#coding=utf-8
from selenium import webdriver
import time
import os

driver = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('frame.html')
driver.get(file_path)

driver.implicitly_wait(30)

#先找到到ifrome1(id = f1)
driver.switch_to_frame("f1")

#再找到其下面的ifrome2(id =f2)

driver.switch_to_frame("f2")

#下面就可以正常的操作元素了
driver.find_element_by_id("kw").send_keys("selenium")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.quit()

switch_to_frame 的参数问题。官方说name 是可以的,但是经过实验发现id 也可以。所以只要frame中id 和name,那么处理起来是比较容易的。如果frame 没有这两个属性的话,你可以直接手动添加。

定位frame 中的对象的更多相关文章

  1. 转:python webdriver API 之定位 frame 中的对象

    在 web 应用中经常会出现 frame 嵌套的应用,假设页面上有 A.B 两个 frame,其中 B 在 A 内,那么定位 B 中的内容则需要先到 A,然后再到 B.switch_to_frame  ...

  2. selenium python (八)定位frame中的对象

    #!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip'#在测试过程中经常遇到frame嵌套的应用,加入页面上有A.B两个fram ...

  3. selenium+python编写自动化脚本时,定位frame中对象操作

    在web应用中经常会出现frame嵌套的应用,假设页面上有A,B两个frame,其中B在A内,那么定位B中的内容则需要先到A,再到B.switchTo().frame方法可以把当前定位的主题切换到fr ...

  4. Python脚本控制的WebDriver 常用操作 <二十四> 定位frame中的元素

    测试用例场景 处理frame需要用到2个方法,分别是switch_to_frame(name_or_id_or_frame_element)和switch_to_default_content() 如 ...

  5. Python+Selenium学习--定位iframe中的对象

    场景 在web 应用中经常会出现frame 嵌套的应用,假设页面上有A.B 两个frame,其中B 在A 内,那么定位B 中的内容则需要先到A,然后再到B.      switch_to_frame ...

  6. Python+Selenium 自动化实现实例-定位frame中的元素

    场景 处理frame需要用到2个方法,分别是switch_to_frame(name_or_id_or_frame_element)和switch_to_default_content() 如何理解这 ...

  7. 定位frame中的元素

    场景 处理frame需要用到2个方法,分别是switch_to_frame(name_or_id_or_frame_element)和switch_to_default_content() 如何理解这 ...

  8. python selenium-webdriver 定位frame中的元素 (十三)

    定位元素时经常会出现定位不到元素,这时候我们需要观察标签的上下文,一般情况下这些定位不到的元素存放在了frame或者放到窗口了,只要我们切入进去就可以很容易定位到元素. 处理frame时主要使用到sw ...

  9. selenium自动化测试入门 定位frame和iframe中的元素对象

    < frame> <iframe> 标签,浏览器会在标签中打开一个特定的页面窗口(框架),它在本窗口中嵌套进入一个网页,当用selenium定位页面元素的时候会遇到定位不到fr ...

随机推荐

  1. 论文笔记之:Large Scale Distributed Semi-Supervised Learning Using Streaming Approximation

    Large Scale Distributed Semi-Supervised Learning Using Streaming Approximation Google  2016.10.06 官方 ...

  2. IT 相关期刊

    码农周刊查阅地址: http://weekly.manong.io/issues/ 快乐码农查阅地址: http://www.codeceo.com/article/category/weekly

  3. Spring中的ApplicationContext事件机制

    ApplicationContext的事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListerner接口来实现. 1. 创建EmailEvent pu ...

  4. [转]centos 下 autoconf版本升级

    首先查看当前版本 #rpm -qf /usr/bin/autoconf autoconf-2.63-5.1.el6.noarch 卸载当前版本 rpm -e --nodeps autoconf-2.6 ...

  5. 转,CV和resume的区别

    一直以来,BBS上的信息资料都传达给我一个网上“主流”的关于CV和resume的看法: CV约等于Resume,前者略倾向于学术,后者略倾向于工作经验,字数控制在1-2页内 说实话,一直以来我也就这么 ...

  6. Nginx基本配置

    1.Nginx官网下载安装程序,http://nginx.org/ 2.启动命令 启动:start nginx.exe停止:nginx -s stop 重新加载: nginx -s reload 3. ...

  7. MysqlServer如何实现成功卸载,并成功安装

    MysqlServer卸载过程如下: (1).在控制面板或者通过其他卸载工具中,卸载MysqlServer. (2).打开C:\ProgramData---这个文件是隐藏的,需显示出来.在里面找到my ...

  8. Linux学习笔记(一)

    1.正则表达式 \w 匹配任何字类字符,包括下划线.与“[A-Za-z0-9_]”等效. \W 与任何非单词字符匹配.与“[^A-Za-z0-9_]”等效. +    一次或多次匹配前面的字符或子表达 ...

  9. Karma 4 - Karma 集成 Webpack 进行单元测试

    可以将 karma 与 webpack 结合起来,自动化整个单元测试过程. 配置环境 1. 首先根据 1 完成基本的 karma 测试环境. 2. 安装 webpack 和 webpack 使用的 l ...

  10. c# 支付宝查单补单

    谁谁谁手机那边支付宝支付成功扣钱了,游戏里面却没有充钱,唯有写个补单程序. 首先要查单,查单接口(交易信息查询接口:https://mapi.alipay.com/gateway.do),服务名称:s ...