Vincent Cheloudiakoff Vincent Cheloudiakoff -3 years ago 46
Ruby Question

Click to all different next pages into a loop until the last page with Watir gem

i have a problem in my ruby watir script.

I want to click through all next pages until the last page, and then puts some first name and last name. I know that the last "next" link is called with one more class "disabled"

stop = b.link(class: 'next-pagination page-link disabled')
.
I try to loop until this classes is reached
break if stop.exists?


loop do
link = b.link(class: 'next-pagination page-link')
name_array = b.divs(class: 'name-and-badge-container').map { |e| e.div(class:'name-container').link(class: 'name-link profile-link').text.split("\n") }
puts name_array
stop = b.link(class: 'next-pagination page-link disabled')
break if stop.exists?
link.click
end


I have this error :


This code has slept for the duration of the default timeout waiting for an Element to exist. If the test is still passing, consider using Element#exists? instead of rescuing UnknownObjectException
/Users/vincentcheloudiakoff/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/watir-6.2.1/lib/watir/elements/element.rb:496:in
rescue in wait_for_exists': timed out after 30 seconds, waiting for #<Watir::Div: located: false; {:class=>"name-and-badge-container", :tag_name=>"div", :index=>13}> to be located (Watir::Exception::UnknownObjectException)
from /Users/vincentcheloudiakoff/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/watir-6.2.1/lib/watir/elements/element.rb:486:in
wait_for_exists'
from /Users/vincentcheloudiakoff/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/watir-6.2.1/lib/watir/elements/element.rb:487:in
wait_for_exists'
from /Users/vincentcheloudiakoff/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/watir-6.2.1/lib/watir/elements/element.rb:487:in
wait_for_exists'
from /Users/vincentcheloudiakoff/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/watir-6.2.1/lib/watir/elements/element.rb:639:in
element_call'
from /Users/vincentcheloudiakoff/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/watir-6.2.1/lib/watir/elements/element.rb:91:in
text'
from /Users/vincentcheloudiakoff/Travail/Automation/lib/linkedin.rb:24:in
block (2 levels) in start'
from /Users/vincentcheloudiakoff/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/watir-6.2.1/lib/watir/element_collection.rb:28:in
each'
from /Users/vincentcheloudiakoff/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/watir-6.2.1/lib/watir/element_collection.rb:28:in
each'
from /Users/vincentcheloudiakoff/Travail/Automation/lib/linkedin.rb:24:in
map'
from /Users/vincentcheloudiakoff/Travail/Automation/lib/linkedin.rb:24:in
block in start'
from /Users/vincentcheloudiakoff/Travail/Automation/lib/linkedin.rb:22:in
loop'
from /Users/vincentcheloudiakoff/Travail/Automation/lib/linkedin.rb:22:in
start'
from start.rb:3:in
'


It clicks on the next page, but does not find the next disabled button.

Answer Source

Use the text to locate that element

b.span(text: 'Suivant').click

You don't have to use parent link and then span like b.link().span() instead you can directly locate span the way I have explained.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download