| How? | HTML Example | Watir Example | Comment |
|---|---|---|---|
| :action | <form action="page.htm"> | ie.form(:action, /page/).click | Used only for form element, specifies the URL where the form is to be submitted. |
| :after? | <a>one</a> <a>two</a> <a>one</a> | ie.link(:after?, ie.link(:text, "two")).click | Finds element after some other element. |
| :alt | <img alt="Continue" /> | ie.image(:alt, "Continue").click | Used to find an element that has an alt attribute (img and input). |
| :class | <a class="header"> | ie.link(:class, "header").click | Used for an element that has a class attribute. |
| :css | |||
| :for | <label for="header"> | ie.label(:for, "header").click | Used only for label element. |
| :href | <a href="page.htm"> | ie.link(:href, /page/).click | Used to identify a link by it's href attribute. |
| :html | <a onclick="new Ajax.Request('/data/?id=227')">add a term</a> | ie.link(:html, /id=227/).click | Used to identify a link by it's HTML. |
| :id | <a id="header"> | ie.link(:id, "header").click | Used to find an element that has an id attribute. Since each id should be unique, according to the XHTML specification, this is recommended as the most reliable method to find an object. |
| :index | <button> | ie.button(:index, 1).click | Used to find the nth element of the specified type on a page. Current versions of Watir use 1-based indexing (starts counting at 1), but future versions will use 0-based indexing (starts counting at 0). |
| :method | <form method="get"> | ie.form(:method, "get").click | Used only for form, the method attribute of a form is either get or post. |
| :name | <a name="header"> | ie.link(:name, "header").click | Used to find an element that has a name attribute. This is useful for older versions of HTML, but name is deprecated in XHTML. |
| :src | <img src="photo.png"> | ie.image(:src, /photo/).click | Used to identify an image by it's URL. |
| :text | <a>click me</a> | ie.link(:text, "click me").click | Used for link, span, div and other elements that contain text. |
| :title | <a title="header"> | ie.link(:title, "header").click | Used for an element that has a title attribute. |
| :url | <a href="page.htm"> | ie.link(:url, /page/).click | :url is synonym for :href. |
| :value | <input value="text"> | ie.text_field(:value, "text").click | Used to find a text field with a given default value, or a button with a given value. |
| :xpath | <a href="page.htm"> | ie.link(:xpath,"//a[@href='page.htm']").click | Finds the item using XPath query. |
| Multiple Attributes | <a>click me</a> <a>click me</a> | ie.link(:text => "click me", :index => 2).click | You can combine more ways of finding elements. This example will click the second link with text click me. |
:id and :name are the quickest of these to process, and so should be used when possible to speed up scripts.
http://wiki.openqa.org/display/WTR/Ways+Available+To+Identify+HTML+Element
本文介绍了一种使用Watir定位Web页面上HTML元素的方法,包括如何通过属性如ID、名称、类名等进行精确查找,这对于自动化测试和网页爬虫开发至关重要。
1458

被折叠的 条评论
为什么被折叠?



