【问题标题】:How to find the data-id in watir?如何在 Watir 中找到 data-id?
【发布时间】:2013-03-22 22:35:41
【问题描述】:

我是新来的水测试。有人可以帮我找到以下元素吗?

<div class="location_picker_type_level" data-loc-type="1">
  <table></table>
</div>

我喜欢找到这个 div, data-loc-type 和 table 存在。

例如:

browser.elements(:xpath,"//div[@class='location_picker_type_section[data-loc-type='1']' table ]").exists?

【问题讨论】:

    标签: watir watir-webdriver


    【解决方案1】:

    Watir 支持使用data- 类型属性作为定位器(即无需使用xpath)。只需将破折号替换为下划线并在开头添加一个冒号即可。

    您可以使用以下方式获取 div(注意属性定位器的格式: data-loc-type -> :data_loc_type ):

    browser.div(:class => 'location_picker_type_level', :data_loc_type => '1')
    

    如果只希望有一个这种类型的 div,您可以通过以下方式检查它是否有一个表:

    div = browser.div(:class => 'location_picker_type_level', :data_loc_type => '1')
    puts div.table.exists?
    #=> true
    

    如果有多个匹配的 div 并且您想检查其中至少一个是否有表格,请使用 any? 方法来获取 divs 集合:

    #Get a collection of all div tags matching the criteria
    divs = browser.divs(:class => 'location_picker_type_level', :data_loc_type => '1')
    
    #Check if the table exists in any of the divs
    puts divs.any?{ |d| d.table.exists? }
    #=> true
    
    #Or if you want to get the div that contains the table, use 'find'
    div = divs.find{ |d| d.table.exists? }
    

    【讨论】:

    • 非常感谢您不仅为回答.. 你解释得很好。我真的很感激你。你就是那个男人……!
    • 还有一个疑问。它就像一个手风琴。它应该只返回真实的扩展。有没有办法找到这个东西?
    • 简短的回答是肯定的。如何将取决于实施。如果表格只在展开时显示,那么您应该将table.exists? 更改为table.present?(检查表格是否存在于 html 中并且对用户可见)。
    • 我们何时添加了对 :data_xxx 选择说明符的支持?太棒了,我认为我们必须对其进行猴子补丁..(修改我的代码)
    • @ChuckvanderLinden,watir-classic 已将其添加到(版本 3)[watir.com/2012/04/24/watir-3-released/]。对于 watir-webdriver,它看起来差不多是同一时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 2017-04-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    相关资源
    最近更新 更多