XPath

一.什么是XPath ?

XPath是 在xml文档中查找信息的语言,可用来在xml文档中对元素和属性进行遍历。XPath 使用路径表达式来选取 XML 文档中的节点或者节点集。Watir对xpath的支持提供给测试人员一种识别页面元素更为有效的解决方案, xpath是较多document对象所支持的识别属性。从原理上说,是把页面HTML转化成为XHTML,然后REXML来解析他,达到使用xpath 表达式语言在文档结构中做查询定位的目的。并且,xpath可以操作html中的扩展tag,或者Watir不支持的tag操作等。Xpath的运用使得 Watir自动化脚本更易于维护。

二.xpath常用路径表达式如下:

// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置

.. 选取当前节点的父节点

.
选取当前节点

@
指定属性


三.简单语法介绍

1.页面代码如:

<area shape="poly"coords="182,32,188,43,190,56,150,56,150,56"href="PieChart.html?category=Critical&pieIndex=0">

Watir语法:

ie.element_by_xpath("//area[contains(@href,'PieChart.html')]/").click

 

2.页面代码如:

<td><img src="3.jpg">Third Image</td>

Watir语法:

ie.cell(:xpath, "//img[@src='3.jpg']/").click()

 

3.页面代码如:

<select foo="bar"> <option value="1">1</option> < /select>

Watir语法:

element = browser.select(:xpath, "//select[@foo='bar']")

 

4.页面代码如:

<a href="test.htm">click me</a>

Watir语法:

ie.link(:xpath,"//a[@href='test.htm']/").clink

5.页面代码如:

 

<table>

  <tr>

    <td><imgsrc="1.jpg">First Image</td>

  </tr>

</table>

<table>

  <tr>

    <td><imgsrc="2.jpg">Second Image</td>

  </tr>

</table>

<table>

  <tr>

    <td><imgsrc="3.jpg">Third Image</td>

  </tr>

</table>

Watir语法:

ie.tables.each do |t|                      # since you don't haveID's, look at every table

  for i in1..t.row_count                  # forevery row in this table

    t[i].each do|cell|                    # for every column in this row, lookat its contents

      ifcell.image(:src, /3.jpg/).exists? # if true, this is your cell

        putscell.text

      end

    end

  end

end

转载:http://www.jointest.org/forum.php?mod=viewthread&tid=56
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值