How to find the data-id in watir?

那年仲夏 提交于 2019-12-05 01:40:45

问题


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?

回答1:


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


来源:https://stackoverflow.com/questions/15570995/how-to-find-the-data-id-in-watir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!