【问题标题】:Ruby Watir, HTMLElement Object indexRuby Watir,HTMLElement 对象索引
【发布时间】:2019-03-17 04:21:07
【问题描述】:

给定以下 WATIR 对象:

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"

但是如何检索索引号?我可以看到它是整数8,但是我找不到返回它的方法。

【问题讨论】:

    标签: ruby selenium watir


    【解决方案1】:

    哈希,{:tag_name=&gt;"a", :index=&gt;8},来自元素的选择器。有一个属性阅读器可以访问这个:

    my_links["Reports"].selector
    #=> {:tag_name=>"a", :index=>8}
    

    你可以通过这个Hash访问索引:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-25
      • 2015-06-22
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      • 2018-08-17
      相关资源
      最近更新 更多