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? }