在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. MyBatis架构(转)

    本文来自http://www.bubuko.com/infodetail-549184.html 如果不太熟悉MyBatis使用的请先参见MyBatis官方文档,这对理解其架构设计和源码分析有很大好处 ...

  2. Hadoop的I/O操作

    HDFS的数据完整性 检验数据是否损坏最常见的措施是:在数据第一次引入系统时计算校验和并在数据通过一个不可靠通道进行传输时再次计算校验和,这样就能发现数据是否被损坏.HDFS会对写入的所有数据计算校验 ...

  3. asp.net 网站访问变慢

    资料一 单个网站解决方法:   把应用程序池回收时间缩短到300-600分钟,其间回收过程中,需要占用一点CPU资源,没办法,为了稳定性,再把回收时间设为凌晨5点. 多网站解决方法: 视服务器网站的多 ...

  4. 【日期-时间】Java中Calendar的使用

    主要介绍了Calendar类的使用 输出 * 时间格式化 * 当前时间:2016-12-02 16:46:27.079 * * 转换:String-->Date-->Calendar * ...

  5. mac idea 设置

    鼠标悬停显示文档注释:preferences->Editor->General:勾选 show quick documentation on mouse move 智能提示模糊搜索:pre ...

  6. android 常见分辨率(mdpi、hdpi 、xhdpi、xxhdpi )屏幕适配

    http://www.tuicool.com/articles/nuyMZb 1 Android手机目前常见的分辨率 1.1 手机常见分辨率: 4:3 VGA     640*480 (Video G ...

  7. [zz]Lessons from Pixar: Why Software Developers Should Be Storytellers

    http://firstround.com/article/lessons-from-pixar-why-software-developers-should-be-story-tellers Whe ...

  8. 把自己主要在做的几个工程都传到了GitHub上

    GitHub链接 https://github.com/MichaelSuen-thePointer 里面有4个项目,一个是我的C大程大作业,一个3600+行的字典程序,已经弃坑不再更新 还有一个叫w ...

  9. 解决sqoop需要输入密码的问题

    修改配置文件:vi /etc/sqoop/conf/sqoop-site.xml <property> <name>sqoop.metastore.client.record. ...

  10. 六、通过插件如何创建自己的MEL command

    1. MAYA API支持不同类型的plugin (1)Command Plugin——扩充MEL命令 (2)Tool Commands——通过鼠标输出 (3)DG plugin——对场景添加新的操作 ...