Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

automation - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...