如何在watir中找到data-id?

Sri*_*Sri 5 watir watir-webdriver

我是watir测试的新手.有人会帮我找到以下元素吗?

<div class="location_picker_type_level" data-loc-type="1">
  <table></table>
</div>
Run Code Online (Sandbox Code Playgroud)

我想找到这个div,data-loc-typetable存在.

例如:

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

Jus*_* Ko 13

Watir支持使用data-类型属性作为定位器(即不需要使用xpath).只需用下划线替换破折号,并在开头添加冒号.

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

browser.div(:class => 'location_picker_type_level', :data_loc_type => '1')
Run Code Online (Sandbox Code Playgroud)

如果只期望有一个这种类型的div,你可以通过这样做来检查它是否有一个表:

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

如果有多个匹配的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? }
Run Code Online (Sandbox Code Playgroud)