Accessing an element with no attributes in Watir

痴心易碎 提交于 2019-12-17 18:59:22

问题


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.


回答1:


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.




回答2:


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/)



回答3:


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.



来源:https://stackoverflow.com/questions/8330926/accessing-an-element-with-no-attributes-in-watir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!