给出以下WATIR对象:
In [1]: s = 'XR0R1R2R3'
In [2]: before = ['R0','R1','R2','R3']
In [3]: after = ['_0','_1','_2','_3']
In [4]: for a, b in zip(before, after):
...: s = s.replace(a, b)
...:
In [5]: s
Out[5]: 'X_0_1_2_3'
my_links["Reports"]
=> #<Watir::Anchor: located: true; {:tag_name=>"a", :index=>8}>
易于检索:
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"
,但是我找不到返回它的方法。
答案 0 :(得分:1)
哈希值{:tag_name=>"a", :index=>8}
来自元素的选择器。有一个属性读取器可以访问它:
my_links["Reports"].selector
#=> {:tag_name=>"a", :index=>8}
您可以从此哈希访问索引:
my_links["Reports"].selector[:index]
#=> 8
请注意,通过集合检索的元素将始终具有索引。可能无法检索单个元素,这意味着索引将为nil
:
browser.link.selector
#=> {:tag_name=>"a"}
browser.link.selector[:index]
#=> nil
但是,如果未指定索引,则可以假定它为零。为避免使用nil
,请提供默认值:
browser.link.selector.fetch(:index, 0)
#=> 0