Looping n rows in table

Hello everyone,

I am trying to do ‘For’ loop for the table but i am having a hard time
as it involves iteration.

Scenario description:

I have a table where there are ‘n’ number of rows but fixed number(5) of
columns as shown below. I need to select the dropdown in column 5 if the
text presents in Column 4.
Just to make clear the rows are infinite and can be any no.

Table name: $b.table(:id, /grid/)

1 2 3 4 5
2 100 200
3 101 201 text dropdown
4 102 202
5 103 203 text dropdown
n 104 204

Here’s what i come up with.

i=0
c=$b.table(:id, /grid/)[i][4]
d=$b.table(:id, /grid/)[i][5]

while i<$b.table(:id, /grid/).row.length
if c.text.exist?==true
$d.select_list.select “dropdown”
else
puts “No value present”
end
end

it doesn’t iterate and gives incorrect results.

Any help would be appreciated.

On Fri, May 20, 2011 at 4:32 PM, Ashok T. [email protected]
wrote:

Just to make clear the rows are infinite and can be any no.

else
puts “No value present”
end
end

it doesn’t iterate and gives incorrect results.

Any help would be appreciated.

Some comments,

You are using $b, which is a global variable. What is the use case? If
you are in the top level (no classes or methods) you don’t need that.
In order to give better advice we would like to know which kind of
object is $b or what is returned by the method $b.table. If it’s an
array of string arrays, you can do this:

table = $b.table(:id, /grid/)
table.each do |row|
if row[3] == “text” #arrays start at 0
puts row[4]
end
end

Let us know if this works for you, or moves you in the right direction.

Jesus.

Thank you Jesus for your gracious reply.

I tried to execute using help from your ruby code. However since the
text keeps on changing, i cannot put the exact text. So i came up with
two options on the code which didn’t work either.

To answer your questions, i am using watir to automate my web app. So i
declared $b = Watir::Browser.new :chrome

I am trying to iterate the table so it is taking $b

Table= $b.table(:id, /grid/)

1 2 3 4 5

2 100 2000

3 150 3000 text dropdown

4 200 4000

5 250 5000 text dropdown

n 300 6000

However, i got the following error message when i used the following
code

Table.rows.each do |row|

row[5].select(“Value from the dropdown”) if row[4].text.exists?

end

Error

C:/Watir Scripts/Project/Debit.rb:58:in block in report’: undefined
methodexists?’ for “Account Number”:String (NoMethodError) from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/element_collection.rb:21:in
each’ from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/element_collection.rb:21:ineach’
from C:/Watir Scripts/Project/Debit.rb:57:in report’ from
driver.rb:36:inrun’ from driver.rb:42:in `’

So i tried to tweak the script and wrote as below.

Table.rows.each do |row|

if row[4].text!= “”

row[5].select(“Value from the dropdown”)

end

Then i got the following error message.
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/container.rb:36:in
extract_selector’: expected Hash or (:how, ‘what’), got [“Exclude:
Duplicate Account”] (ArgumentError) from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/elements/generated.rb:2163:inselect’
from C:/Watir Scripts/Project/Credit.rb:53:in block in report’ from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/element_collection.rb:21:ineach’
from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/element_collection.rb:21:in
each’ from C:/Watir Scripts/Project/Debit.rb:51:inreport’ from
driver.rb:36:in run’ from driver.rb:42:in’

So i think we need to use (how, what) expressions, can you help me how
to use those expressions in ‘each’ command?

On Sat, May 21, 2011 at 8:08 PM, Ashok T. [email protected]
wrote:

5 250 5000 text dropdown
end

Then i got the following error message.

So i think we need to use (how, what) expressions, can you help me how
to use those expressions in ‘each’ command?

I don’t know much about Watir, but seeing that the error is in
container.rb, I looked here:

http://rdoc.info/gems/watir/1.8.1/Watir/Container:select_list

There you can find information about what :how and “what” mean, and
which values you can pass there.

Jesus.

Thank you very much Jesus again for your reply.

When i looked at the ruby doc, it says how and what means the ID of the
object and it’s name.

so here’s my code that i wrote

Table.rows.each do |row|

if row[4].text!= “”

row[5].select_list(:id, /GridList/).select(“Value from the dropdown”)

end

end

But i’m still getting the error message as shown below.

C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/elements/element.rb:241:in
assert_exists': unable to locate element, using {:id=>/dispositionList/, :tag_name=>"select"} (Watir::Exception::UnknownObjectException) from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/elements/select.rb:125:inselect_by’
from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/elements/select.rb:64:in
select' from C:/Watir Scripts/Project/Credit.rb:60:inblock in
liability_report’
from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/element_collection.rb:21:in
each' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/element_collection.rb:21:ineach’
from C:/Watir Scripts/Project/Credit.rb:58:in liability_report' from driver.rb:36:inrun’
from driver.rb:42:in `’

Any help further would be highly appreciated.

Thanks

Ash

On Mon, May 23, 2011 at 5:21 PM, Ashok T. [email protected]
wrote:

{:id=>/dispositionList/, :tag_name=>“select”}

C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/element_collection.rb:21:in

`each’
from

C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdriver/element_collection.rb:21:in

each' from C:/Watir Scripts/Project/Credit.rb:58:in liability_report’
from driver.rb:36:in run' from driver.rb:42:in

Well, it looks like in your Credit.rb, in line 58 you are looking for
an element with id matching /dispositionList/, and it can’t find it.
We can’t help more without looking at the HTML page you are parsing,
and the exact code you are using.

Jesus.

Thank you Jesus, i figured out the answer for that issue. I used regular
expressions and it worked perfectly. The code is shown below.

Browser=$b.table(:id, /Grid/)
$Browser.rows.each do |row|
if (row[4].text =~ /Acct(.*) /)
puts “Dropdown selected”
row[5].select_list(:id, /dispositionList/).select “Value from
dropdown”
end
end