I'm trying to iterate over links' array with Capybara. It's Yahoo main page and i'm trying to successively check all links from the left side bar('Mail', 'News', 'Sports' etc). Here is the piece of html('Mail'):
<a class="ell fz-s " href="http://hsrd.yahoo.com/_ylt=A2KLtiE7CQZVoV8AGBmbvZx4/RV=1/RE=1427668539/RH=aHNyZC55YWhvby5jb20-/RO=2/RU=aHR0cDovL3Nwb3J0cy55YWhvby5jb20v/RS=^ADA0Gc4IcWXarglWyV.UMCa7fh5TLA-"> <i id="nav-sports" class="img-sprite"></i><span>Sports</span></a>
I'm trying to push all links elements to an array and then visit each of them:
page.all('.ell.fz-s').each { |el|
link = el[:href]
visit(link)
}
But it works only for the first link and then stops. What am i doing wrong?
You should store hrefs as Taryn East suggested.
But instead of visiting just link you have to visit full url.
links.each do |link|
visit(url + link)
end
Hope it'll help someone.
Right.. so the problem is that after you visit the first link... you are now on a different page with different links so referring to the links after that just breaks (the reference to the old links is stale)
you probably need to pull out all the links first - then start visiting them eg try something like:
# pull them out of the page and stuff the actual links away in a separate array
links = page.all('.ell.fz-s').map { |el| el[:href] }
puts links.inspect
links.each do |link|
visit link
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.