How to find data id in watir?

I am new to testing. Someone help me find the next item.

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

I like to find it div, data-loc-typep table.

Example:

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

Watir supports type attributes data-as locators (i.e. no need to use xpath). Just replace the dashes with underscores and add a colon to the beginning.

You can get the div using the following (note the locator format for the attribute: data-loc-type →: data_loc_type):

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

If it is 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

divs, , , , 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? }
+12

All Articles