原文链接:http://stackoverflow.com/questions/5721438/using-watir-webdriver-how-can-i-click-a-table-row
Question:
I'm trying to convert my old watir scripts to use watir-webdriver instead, as that will (eventually) support IE9 and Firefox 4. $browser.table_row(:id => "account_1").click
is what I used to click the first row of a table on screen in watir/firewatir, but the API for this was modified in watir-webdriver. Now, the code is this: $browser.table(:class => "sortable")[0].click
which should grab the first row of the table then click it. It seems to be successful, as it continues code execution, but it doesn't actually click the row.
Can someone explain what the right syntax would be in this case?
Here's the source code around the area I want to click:
<table class="sortable">
<thead>
<tr id="">
<th> </th>
<th class="sort" > Name </th>
<th class="sort" > Number </th>
</tr>
</thead>
<tbody>
<tr id="account_1" onclick=";$('timer').show();; new Ajax.Request('create_new_account', {asynchronous:false, evalScripts:true, onComplete:function(request){;$('timer').hide();initializeCustomEffects();}})">
<td></td>
<td class="sortTd">Test Account</td>
<td class="sortTd">1</td>
</tr>
</tbody>
Answer:
A1:
our code is finding the first row of the table, which is inside the<thead>
and does not have an onclick handler. Try this instead:
browser.tr(:id => "account_1").click
Here's a script that demonstrates the behavior, and here's an overview of the revised table API.
A2:A Row isn't an HTTP object that would normally respond to a click. Is there something inside the row like a link that you want to click on, or is the row itself setup using event handling to respond to a particular event such as 'onclick' or 'onmousedown' ??
If the former, try actually clicking on the object within the row. If the latter then try experimenting with the .fire_event method and different events.
For example:
browser.table(:class => "sortable")[0].fire_event("onmousedown")
or maybe
browser.table(:class => "sortable").row(:index, 0).fire_event("onmousedown")
(addendum) AH now that we have the HTML we can see where the onclick handler is and as pointed out by Jarib, you were clicking on the header row (which is technically the first row of the table) To click the thing you want, you need something along the lines of
browser.row(:id, "account_1").click
browser.row(:text, /Test Account/).click
browser.table(:class => "sortable").row(:index, 1).fire_event("onclick")
(unless your intent is to sort the table, in which case I suspect you need to click a CELL in the first (header) row in order to sort the table on that column.
browser.cell(:text, ' Name ').click