简体   繁体   English

无法使用Capybara遍历链接的数组

[英]Can't iterate over links' array with Capybara

I'm trying to iterate over links' array with Capybara. 我正在尝试使用Capybara遍历链接的数组。 It's Yahoo main page and i'm trying to successively check all links from the left side bar('Mail', 'News', 'Sports' etc). 这是Yahoo主页,我正在尝试从左侧栏中依次检查所有链接(“邮件”,“新闻”,“体育”等)。 Here is the piece of html('Mail'): 这是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. 您应该按照Taryn East的建议存储hrefs。

But instead of visiting just link you have to visit full url. 但是,您不仅要访问链接,还必须访问完整的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM