Using watir-webdriver, how can I click a table row?

探讨了从Watir转换到Watir-WebDriver时遇到的表格点击问题,并提供了有效的解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接: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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值