XPath在watir自动化测试中的应用
http://www.51testing.com/?170805/action_viewspace_itemid_88589.html
ie.element_by_xpath("//area[contains(@href , 'PieChart.html')]/").click
<table>
<tr>
<td><img src="1.jpg">First Image</td>
</tr>
</table>
<table>
<tr>
<td><img src="2.jpg">Second Image</td>
</tr>
</table>
<table>
<tr>
<td><img src="3.jpg">Third Image</td>
</tr>
</table>
若不用xpath,实现只点击3.jpg的方法:
ie.tables.each do |t| # since you don't have ID's, look at every table
for i in 1..t.row_count # for every row in this table
t[i].each do |cell| # for every column in this row, look at its contents
if cell.image(:src, /3.jpg/).exists? # if true, this is your cell
puts cell.text
end
end
end
end
但是用xpath就是非常简练:
ie.cell(:xpath, "//img[@src='3.jpg']/").click()