Ruby Watir – 尝试遍历cnn.com中的链接并单击其中的每个链接

我创建了这个方法来遍历网站中某个div中的链接。 我的方法的一个方法是收集链接将它们插入一个数组然后单击它们中的每一个。

require 'watir-webdriver' require 'watir-webdriver/wait' site = Watir::Browser.new :chrome url = "http://www.cnn.com/" site.goto url box = Array.new container = site.div(class: "column zn__column--idx-1") wanted_links = container.links box << wanted_links wanted_links.each do |link| link.click site.goto url site.div(id: "nav__plain-header").wait_until_present end site.close 

到目前为止,似乎我只能点击第一个链接,然后我收到一条错误消息,说明:

  unable to locate element, using {:element=>#} (Watir::Exception::UnknownObjectException) 

我对ruby很新。 我感谢任何帮助。 谢谢。

问题是,一旦您导航到另一个页面,所有元素引用(即wanted_links中的元素引用)都会变得陈旧。 即使您返回同一页面,Watir / Selenium也不知道它是同一页面,也不知道存储的元素在哪里。

如果您要离开,则需要先收集所需的所有数据。 在这种情况下,您只需要href值。

 # Collect the href of each link wanted_links = container.links.map(&:href) # You have each page URL, so you can navigate directly without returning to the homepage wanted_links.each do |link| site.goto url end 

如果链接没有直接导航到页面(例如,他们在单击时执行JavaScript),您将需要收集足够的数据以便稍后重新定位元素。 您用作定位器的内容取决于已知的静态/唯一。 作为一个例子,我将假设链接文本是一个很好的定位器。

 # Collect the text of each link wanted_links = container.links.map(&:text) # Iterate through the links wanted_links.each do |link_text| container = site.div(class: "column zn__column--idx-1") container.link(text: link_text).click site.back end