首页 技术 正文
技术 2022年11月13日
0 收藏 654 点赞 5,065 浏览 1917 个字

在 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>
<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>
<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 ,两个文件和我们的脚本文件放同一个目录下,通过浏览器打开,得到
下列页面:
转:python webdriver API 之定位 frame 中的对象
图 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 没有这两个属性的话,你可以直接手动添加。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,893