[英]Ruby Watir, HTMLElement Object index
GIVEN the following WATIR object: 给出以下WATIR对象:
my_links["Reports"]
=> #<Watir::Anchor: located: true; {:tag_name=>"a", :index=>8}>
The tag_name
is easy to retrieve: tag_name
易于检索:
my_links["Reports"].tag_name
2018-10-12 12:29:00 INFO Watir <- `Verifying precondition #<Watir::Anchor: located: true; {:tag_name=>"a", :index=>8}># for tag_name`
2018-10-12 12:29:00 INFO Watir <- `Verified precondition #<Watir::Anchor: located: true; {:tag_name=>"a", :index=>8}>#assert_exists`
2018-10-12 12:29:00 INFO Watir -> `Executing #<Watir::Anchor: located: true; {:tag_name=>"a", :index=>8}>#tag_name`
2018-10-12 12:29:00 INFO Watir <- `Completed #<Watir::Anchor: located: true; {:tag_name=>"a", :index=>8}>#tag_name`
=> "a"
But how do I retrieve the index number? 但是,如何检索索引号? I can see it is the integer
8
, but I can't find a method to return it. 我可以看到它是整数
8
,但是找不到返回它的方法。
The Hash, {:tag_name=>"a", :index=>8}
, comes from the element's selector. 哈希值
{:tag_name=>"a", :index=>8}
来自元素的选择器。 There is an attribute reader to access this: 有一个属性读取器可以访问它:
my_links["Reports"].selector
#=> {:tag_name=>"a", :index=>8}
You can access the index from this Hash: 您可以从以下哈希访问索引:
my_links["Reports"].selector[:index]
#=> 8
Note that elements retrieved via a collection will always have an index. 请注意,通过集合检索的元素将始终具有索引。 Retrieving an individual element may not, which means the index will be
nil
: 不能检索单个元素,这意味着索引将为
nil
:
browser.link.selector
#=> {:tag_name=>"a"}
browser.link.selector[:index]
#=> nil
However, if the index isn't specified, you can assume it's zero. 但是,如果未指定索引,则可以假定它为零。 To avoid the
nil
, provide a default value: 为了避免
nil
,请提供默认值:
browser.link.selector.fetch(:index, 0)
#=> 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.