Friday, April 9, 2010

Watir: How to identify Links ?

You can use Watir to click links in a variety of ways. Watir can click links by looking at the link text you see in the browser or by looking at the attributes available in the HTML tag. Common attributes are id, name and href. For complete list see HTML Elements Supported by Watir.
Here is an example using a link to the Pickaxe book by the Pragmatic Programmers:
What you see in the web browser:
Pickaxe
What you see in the HTML source:

id Attribute

Watir code to click a link using the id attribute:
ie.link(:id, "one").click

name Attribute

Watir code to click a link using the name attribute:
ie.link(:name, "book").click

Text

Watir code to click a link using link's text:
ie.link(:text, "Pickaxe").click

href Attribute

Watir code to click a link using the href attribute:
ie.link(:href, "http://pragmaticprogrammer.com/titles/ruby/").click

No comments:

Post a Comment