码迷,mamicode.com
首页 > 编程语言 > 详细

Selenium定位HTML元素(Python)

时间:2015-03-04 20:42:53      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:

本篇不是介绍通过各种方法(id, name, tag_name, css, xpath等)定位HTML各元素的普及性文章,网络上各种文档和博客介绍得均比较全面。这里只是将自己实践中,遇到的个别问题及解决方案进行记录。

 

在此推荐Selenium官方文档(Python版):http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html

可以搜索关键字,得到函数用法和源码。

 

下面记录定位HTML元素方面遇到的问题和解决方法(持续更新)。

 

1、无id的frame定位

问题描述:需要定位frame内的元素,但此frame无id。

解决方法:swich_to_frame( )函数定义如下:

 1 def switch_to_frame(self, frame_reference):
 2         """
 3         Switches focus to the specified frame, by index, name, or webelement.
 4 
 5         :Args:
 6          - frame_reference: The name of the window to switch to, an integer representing the index,
 7                             or a webelement that is an (i)frame to switch to.
 8 
 9         :Usage:
10             driver.switch_to_frame(‘frame_name‘)
11             driver.switch_to_frame(1)
12             driver.switch_to_frame(driver.find_elements_by_tag_name("iframe")[0])
13         """
14         self.execute(Command.SWITCH_TO_FRAME, {id: frame_reference})

可知,参数frame_reference有3种形式。对于无id的frame,即可通过后2种参数,定位到此frame。相对来说,后2种参数更像是通过一个页面中所有frame的“相对位置”,来定位具体某一个frame;而第1种参数则是通过绝对的”名“来定位。

Selenium定位HTML元素(Python)

标签:

原文地址:http://www.cnblogs.com/kevin1201/p/4314057.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!