Accessing an element with no attributes in Watir
Asked Answered
A

3

6

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.

Astound answered 30/11, 2011 at 18:24 Comment(0)
Y
11

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.

York answered 30/11, 2011 at 19:11 Comment(1)
Note that it is only the FIRST span if you are using older versions of Watir which use one based indexing, with Watir 2.x or Watir-Webdriver which use zero based indexing (like Ruby) :index =>1 would be the Second instance of a span within the divWaiwaif
A
9

If it has text:

browser.span(:text => "Text")

If you know only part of the text you can use regular expression:

browser.span(:text => /Text/)
Asafoetida answered 30/11, 2011 at 20:29 Comment(0)
W
8

There are basically three ways to address this particular challenge. Zeljko has addressed the first which is based on what is inside the element such as known text. Adam addresses the most common way, what is enclosing or containing the element I'll address the third way, which is what is enclosed-by or beside the element.

If you have a known element that is inside the one you want, then you can start with that and use the .parent method to get the 'container' element. This can also be used to find a 'sibling' element by using .parent to get to the one you want via a common container such as a table row. The first use is fairly obvious, but the second is probably more common and very useful when working with tables.

For example Lets say you have a table with multiple rows of data where one column is unique part numbers, and another column has "add to cart" links. Now, if you want to add a specific part to your cart, you could use Index combined with the text 'add to cart' using code like this based on it being the 5th link with that specific text

browser.link(:text => 'add to cart', :index => 4).click

But this is brittle because as soon as the results change, (which can happen a lot with live data) your part is no longer the 5th one in that table, and your test would break. You would need some verification you've found the correct part and not something else on that row. However, in watir you can do something like this:

browser.cell(:text => 'Part no. 123-45').parent.link(:text => 'add to cart').click

In the case of a table cell, the parent of the cell will usually be a table row, and thus in plain english this translates to 'find the cell with 'part no 123-45' in it, and then in that same row find and click on the 'add to cart' link. (although I'm guessing you figured that out just by reading the code.)

You can use this to get any 'sibling' or even just the 'parent' itself where there's some unique element next to or within the object you need to interact with.

You can probably do something similar to that with Xpath, but good luck making any sense out of it when reading the code five weeks later. This is one reason I vastly prefer Watir and Watir-Webdriver vs Selenium.

Waiwaif answered 30/11, 2011 at 21:15 Comment(4)
I never could have imagined so many answers to a simple problem.Covered
A simple problem, but presented in a very general form without any context or surrounding HTML. Hence the variety of answers each of which is far less general than the question. Mine is admittedly addressing a more narrow niche.. Hmmm a useful edit comes to mind.Waiwaif
@DaveMcNulla, Does my revised answer put the other answers into a little more context now?Waiwaif
That does make sense of the three different answers. I also appreciate the lesson I got about using the relationship in the objects to find and manipulate in lieu of the xpath solution.Covered

© 2022 - 2024 — McMap. All rights reserved.