selenium python (八)定位frame中的對象


#!/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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM