I am trying to click on a button in a table cell that has a dynamic name with a prefix of button_keep

this is the unique path that firebug has pointed out for the table cell.

#mergePatientsSelectedTable > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(1)

I'm trying to get there using the following but it does not find the button.

b.table(:id => "mergePatientsSelectedTable").tbody{2}.tr{1}.td{2}.table{1}.tbody{1}.tr{1}.td{1}.button(:index => 0).button(:name => /button_keep/).click

I have tried to flash on the cells and use IRB yet it continues to give me an assert that cannot find the button.

Any suggestions . thank you

有帮助吗?

解决方案

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.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top