#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = 'zuoanvip'
#在测试过程中经常遇到frame嵌套的应用,加入页面上有A、B两个frame,B在A内,那么要找到B中的元素,则首先应找到A然后再到B。可以通过switch_to_frame
from selenium import webdriver
import os
#打开页面
driver = webdriver.Firefox()
file_path = 'file:///' + os.path.abspath('frame.heml')
driver.get(file_path)
#等待30s
driver.implicitly_wait(30)
#先找到id=f1的frame
driver.switch_to_frame('f1')
#再找到id=f2的frame
driver.switch_to_frame('f2')
#接下来定位frame2里面的元素
driver.find_element_by_id('kw').send_keys('selenium')
driver.find_element_by_id('su').click()
==========================================================================================
frame的源代码:
<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>
<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>