Watir识别HTML元素的方法及watir api补充
Watir识别HTML元素的方法地址:
http://wiki.openqa.org/display/WTR/Ways+Available+To+Identify+HTML+Element
Watir Api补充
button |
<input> tags with type=button, submit, image or reset |
radio |
<input> tags with the type=radio; known as radio buttons |
check_box |
<input> tags with type=checkbox |
text_field |
<input> tags with the type=text (single-line), type=textarea (multi-line), and type=password |
hidden |
<input> tags with type=hidden |
select_list |
<select> tags, known as drop-downs or drop-down lists |
label |
<label> tags (including "for" attribute) |
span |
<span> tags |
div |
<div> tags |
p |
<p> (paragraph) tags |
link |
<a> (anchor) tags |
table |
<table> tags, including row and cell methods for accessing nested elements |
image |
<img> tags |
form |
<form> tags |
frame |
frames, including both the <frame> elements and the corresponding pages |
map |
<map> tags |
area |
<area> tags |
li |
<li> tags |
h1 - h6 |
<h1>, <h2>, <h3>, <h4>, <h5>, <h6> tags |
How? |
HTML源码 |
Watir代码 |
说明 |
:action |
<form action=”page.htm”> |
ie.form(:action, /page/).click |
只可用于form元素, |
:after? |
<a>one</a> <a>two</a> <a>one</a> |
ie.link(:after?, ie.link(:text, “two”)).click |
定位到指定元素之后的页面控件 |
:class |
<a> |
ie.link(:class, “header”).click |
当元素有class属性时可以使用其进行定位、识别 |
:for |
<label for=”header”> |
ie.label(:for, “header”).click |
只可用于定位label元素 |
:href |
<a href=”page.htm”> |
ie.link(:href, /page/).click |
可使用href属性定位超链接 |
:id |
<a> |
ie.link(:id, “header”).click |
用于通过id识别控件,由于xhtml说明中定义了id唯一,推荐使用这种最为可靠的识别方式 |
:index |
<button> |
ie.button(:index, 1).click |
用于定位页面指定类别第N个元素。目前的Watir版本中从index为1开始计数,但以后可能会从0开始计数。 |
:method |
<form method=”get”> |
ie.form(:method, “get”).click |
仅可用于定位form元素,form的method属性值可以是get或post |
:name |
<a> |
ie.link(:name, “header”).click |
用于通过name属性识别元素,对于之前版本的HTML比较有用,但对于XHTML来说识别度较低。 |
:src |
<img src=”photo.png”> |
ie.image(:src, /photo/).click |
当需要通过URL来识别一个图片元素时使用 |
:text |
<a>click me</a> |
ie.link(:text, “click me”).click |
可用于识别包含文本的页面元素,如link,span,div等 |
:title |
<a title=”header”> |
ie.link(:title, “header”).click |
识别含有title属性的元素 |
:value |
<input value=”text”> |
ie.text_field(:value, “text”).click |
用于识别含有默认值的text field元素或button元素 |
:url |
<a href=”page.htm”> |
ie.link(:url, /page/).click |
url属性和href属性相同 |
xpath |
<a href=”page.htm”> |
ie.link(:xpath,”//a[@href='page.htm']“).click |
使用xpath定位元素 |
multiple attribute |
<a>click me</a> <a>click me</a> |
ie.link(:text => “click me”, :index => 2).click |
可以使用多个属性联合定位识别,如本例所示,将点击文本为”click me”的第二个链接 |
例如:browser.links.each do |link|
puts link.href
end
Watir Method |
|
areas |
|
buttons |
<button> <input type="button"> <input type="image"> <input type="reset"> <input type="submit"> |
checkboxes |
|
dds |
|
divs |
|
dls |
|
dts |
|
ems |
|
file_fields |
|
forms |
|
? |
|
? |
|
hiddens |
|
images |
|
labels |
|
lis |
|
links |
|
maps |
|
ps |
|
pres |
|
radios |
|
select_lists |
|
spans |
|
strongs |
|
tables |
|
bodies |
|
cells |
|
rows |
|
text_fields |
<input> <input type="password"> <input type="text"> <textarea> |
uls |
识别元素所支持的方法,地址:
http://wiki.openqa.org/display/WTR/HTML+Elements+Supported+by+Watir
更多资料请访问以下地址:
http://wiki.openqa.org/display/WTR/Tutorial
http://www.w3school.com.cn/xpath/xpath_syntax.asp