如何在watir中找到数据ID? [英] How to find the data-id in watir?
问题描述
我是 watir 测试的新手.有人能帮我找到以下元素吗.
<table></table>
我喜欢找到这个div
,data-loc-type
和table
存在.
例如:
browser.elements(:xpath,"//div[@class='location_picker_type_section[data-loc-type='1']' table ]").存在吗?
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')把div.table.exists?#=>真的
如果有多个匹配的 div 并且您想检查其中至少一个具有该表,请对 divs
集合使用 any?
方法:
#获取所有符合条件的div标签的集合divs = browser.divs(:class => 'location_picker_type_level', :data_loc_type => '1')#检查表是否存在于任何一个div中把 divs.any?{ |d|d.table.exists?}#=>真的#或者如果你想获取包含表格的div,使用'find'div = divs.find{ |d|d.table.exists?}
I am new to watir testing. Would somebody help me to find the following element.
<div class="location_picker_type_level" data-loc-type="1">
<table></table>
</div>
I like to find this div
, data-loc-type
with table
exists.
ex:
browser.elements(:xpath,"//div[@class='location_picker_type_section[data-loc-type='1']' table ]").exists?
Watir supports using data-
type attributes as locators (ie no need to use xpath). Simply replace the dashes with underscores and add a colon to the start.
You can get the div using the following (note the format of the locator for the attribute: data-loc-type -> :data_loc_type ):
browser.div(:class => 'location_picker_type_level', :data_loc_type => '1')
If it is only expected that there is one div of this type, you can check that it has a table by doing:
div = browser.div(:class => 'location_picker_type_level', :data_loc_type => '1')
puts div.table.exists?
#=> true
If there are multiple divs that match and you want to check that at least one of them has the table, use the any?
method for a divs
collection:
#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? }
这篇关于如何在watir中找到数据ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!