You do not need to write out the whole path. Each method/locator looks in all descendents, not just direct children. Using entire paths can make the code quite brittle.
So why not just locate the button based on the known attributes:
b.table(:id => "mergePatientsSelectedTable").button(:name => /button_keep/).click
But to explain why you were having problems with your solution, doing tbody{2}
actually returns the first tbody element (not the second). The {2}
is a block that gets ignored.
For example, consider the html:
<div>hi</div>
<div>bye</div>
You can see the first div gets returned when using a block:
b.div{2}.text
#=> "hi"
To get the second div, you can either use the index locator or get the second element of the collection:
b.div(:index => 1).text
#=> "bye"
b.divs[1].text
#=> "bye"
So if you really wanted to do the entire path, you could have done:
b.table(:id => "mergePatientsSelectedTable").tbody(:index => 1).tr.td(:index => 1).table.tbody.tr.td.button(:name => /button_keep/).click
Note that:
nth-child
is 1-based index while Watir uses a 0-based index.- If you want the first match, you do not need to include the index -
:index => 0
is assumed.