How to access cell or row index using watir-webdriver

958 views
Skip to first unread message

Dan Claudiu Pop

unread,
May 5, 2011, 4:31:29 AM5/5/11
to Watir General
Hello,

Can you guide me with some general approaches to get the index of a
row or cell with watir-webdriver ?

I've tried some approaches like:

@browser.tbody(:index, 0).rows.each do |row|
if row.text.include?("#{name}") then
puts row.row_index
end
end

And also i tried with "each_with_index" but didn't get the expected
output.

Thank you,
Dan

Željko Filipin

unread,
May 5, 2011, 4:35:27 AM5/5/11
to watir-...@googlegroups.com
On Thu, May 5, 2011 at 10:31 AM, Dan Claudiu Pop <dancla...@gmail.com> wrote:
> And also i tried with "each_with_index" but didn't get the expected

What did you get and what did you expect? Please also provide your code.

Rubydoc says watir-webdriver does not have a method with "index" in name:

http://rubydoc.info/gems/watir-webdriver/

Željko
--
watir.com - community manager
watir.com/book - author
watirpodcast.com - host
viaqa.mobi conference on software testing - organizer


Dan Claudiu Pop

unread,
May 5, 2011, 5:23:59 AM5/5/11
to Watir General
I've used (a solution posted by you):

@browser.tbody(:index, 0).rows.each_with_index do |row, index|
row.cells.each do |cell|
if cell.text == "some_text"
puts @browser.tbody(:index, 0)[index]
break
end
end
end

It returns: #<Watir::TableRow:0x1214726>. I want to return the actual
row index/number. I can do with a count but didn't wanted that
approach.

What i am trying to accomplish is to click delete/update (which id on
url is generated dynamically) based on a cell which i can identify by
text, therefore deleting/updating the corresponding row.

Here is the UI => http://i52.tinypic.com/2yu12jd.png
And DOM: => http://i53.tinypic.com/2bwo5t.png

Any other approach would be welcome:

Maybe something like (didn't get this work): @browser.td(:text =>
"text_name").td(:xpath => ""//a[@href=/delete/").click


On May 5, 11:35 am, Željko Filipin <zeljko.fili...@wa-research.ch>
wrote:
> On Thu, May 5, 2011 at 10:31 AM, Dan Claudiu Pop <danclaudiu...@gmail.com>

Željko Filipin

unread,
May 5, 2011, 5:27:34 AM5/5/11
to watir-...@googlegroups.com
On Thu, May 5, 2011 at 11:23 AM, Dan Claudiu Pop <dancla...@gmail.com> wrote:
>      puts @browser.tbody(:index, 0)[index]
> It returns: #<Watir::TableRow:0x1214726>. I want to return the actual
> row  index/number.

Try this:

puts index

instead of:


puts @browser.tbody(:index, 0)[index]

Željko

Dan Claudiu Pop

unread,
May 5, 2011, 6:56:59 AM5/5/11
to Watir General
Yep, works just fine :)

Gracias,
Dan

On May 5, 12:27 pm, Željko Filipin <zeljko.fili...@wa-research.ch>
wrote:
> On Thu, May 5, 2011 at 11:23 AM, Dan Claudiu Pop <danclaudiu...@gmail.com>

Jari Bakken

unread,
May 5, 2011, 11:54:50 AM5/5/11
to watir-...@googlegroups.com
On Thu, May 5, 2011 at 10:31 AM, Dan Claudiu Pop
<dancla...@gmail.com> wrote:

> And also i tried with "each_with_index" but didn't get the expected
> output.
>

That should work.

Reply all
Reply to author
Forward
0 new messages