在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. SQL Server DBA日常查询视图_数据库性能视图

    1.获取有关按平均CPU 时间排在最前面的五个查询的信息 total_worker_time/execution_count AS [Avg CPU Time], ), ((CASE qs.state ...

  2. SpringMVC学习系列(10) 之 异常处理

    在项目中如何处理出现的异常,在每个可能出现异常的地方都写代码捕捉异常?这显然是不合理的,当项目越来越大是也是不可维护的.那么如何保证我们处理异常的代码精简且便于维护呢?这就是本篇要讲的内容—>异 ...

  3. boost-asio-cpp-network-programming阅读笔记

    第二章:boost.asio 的基本原理 网络api boost.asio的命名空间 IP地址 端点 sockets 同步错误代码 socket成员函数 其他注意事项 read/write/conne ...

  4. 关于iscroll阻止浏览器默认动作

    使用iscroll时,移动端遇到需要长按复制功能,但是iscroll屏蔽了浏览器默认事件,所以实现不了. 解决方案: myScroll = new IScroll('#wrapper',{ preve ...

  5. nodejs初探(二)第一个nodejs程序“hello world”

    直接用文本编辑器编写helloworld.js,保存在桌面 var http = require("http"); http.createServer(function(reque ...

  6. Linux nfs配置

    ***************节点2的arch2目录挂载到节点1下.那么节点2是主节点1是备******** 10.230.39.234(节点1)cat /etc/exports /arch2 10. ...

  7. Navicat链接Oracle提示ORA-12737

    ORA-12737: Instant Client Light: unsupported server character set string Cause: The character set sp ...

  8. hibernate的延迟加载及其与session关闭的矛盾

    延迟加载就是并不是在读取的时候就把数据加载进来,而是等到使用时再加载. 那么Hibernate是怎么知道用户在什么时候使用数据了呢?又是如何加载数据呢? 其实很简单,它使用了代理机制.返回给用户的并不 ...

  9. MFC webbrowser读取文档的meta分析

    IDispatch* pDisp = NULL; IDispatch* pDisp2 = NULL; IHTMLDocument2 *pHtmlDoc2 = NULL; IHTMLElementCol ...

  10. json(http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/24/3096437.html)

    http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/24/3096437.html