Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing an element with no attributes in Watir

Using Watir, is there a way to access an element without attributes?

For example:

<span>Text</span>

I'd like to avoid using xpath, but if that's the only way it's cool.

like image 957
carlmonday Avatar asked Nov 30 '11 18:11

carlmonday


People also ask

How do you select an element in Watir?

Elements are located by creating a Selector Hash, which Watir translates into the potentially complicated information the driver needs to know to identify the element. The special cases will be highlighted below, but Watir Locators: Accept String values for exact matching. Accept RegExp values for partial matching.

What is Watir WebDriver gem?

Watir-WebDriver (Watir is short for Web Application Testing in Ruby) is a Ruby gem which allows you to automate your browser (make it click a button, submit a form, wait for some text to appear before continuing, and so on).


1 Answers

Disregarding the non-WATIR issues of having tags in the first place, or requesting unique attributes from your developers (or yourself), you can always access an element via its parent elements, or by index.

For example: Text

@browser.div(:name => "content").span(:index => 1)
#this is the first span element inside this div

You can work through however many unique elements you need to before reaching the child span element, without using Xpath. Of course, you only need one unique parent element to reach that specific child element, and you work down from that to the child.

div(:how => what).table(:how => what).td(:how => what).span(:how => what).text

Another example, assuming it is the nth span on the page: @browser.span(:index => n)

The by-index approach is very brittle and prone to breaking when any update is made to the page, however.

like image 149
adam reed Avatar answered Oct 11 '22 04:10

adam reed