XPath

来源:互联网 发布:dnf闪光网络中断 编辑:程序博客网 时间:2024/05/29 15:25

一.什么是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
原创粉丝点击