Looping n rows in table

A

Ashok T.

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/)[4]
d=$b.table:)id, /grid/)[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.
 
A

Ashok T.

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?
 
J

Jesús Gabriel y Galán

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?

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.
 
A

Ashok T.

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:in
`select_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:in `block 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:in
`each'
from C:/Watir Scripts/Project/Credit.rb:58:in `liability_report'
from driver.rb:36:in `run'
from driver.rb:42:in `<main>'

Any help further would be highly appreciated.

Thanks

Ash
 
J

Jesús Gabriel y Galán

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!=3D ""

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-webdr= iver/elements/element.rb:241:in
`assert_exists': unable to locate element, using
{:id=3D>/dispositionList/, :tag_name=3D>"select"}
(Watir::Exception::UnknownObjectException)
=A0from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdr= iver/elements/select.rb:125:in
`select_by'
=A0from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdr=
iver/elements/select.rb:64:in
`select'
=A0from C:/Watir Scripts/Project/Credit.rb:60:in `block in
liability_report'
=A0from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdr= iver/element_collection.rb:21:in
`each'
=A0from
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.3/lib/watir-webdr=
iver/element_collection.rb:21:in
`each'
=A0from C:/Watir Scripts/Project/Credit.rb:58:in `liability_report'
=A0from driver.rb:36:in `run'
=A0from driver.rb:42:in `<main>'

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.
 
A

Ashok T.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top