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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With