【问题标题】:Ruby wait until external request finishedRuby 等到外部请求完成
【发布时间】:2016-06-08 04:03:28
【问题描述】:

我正在使用 Watir 和 phantomjs 来抓取页面。我注意到它会抛出一个NoMethodError: undefined method 'attributes' for nil:NilClass 在控制台中运行它时,但是如果我将调试器放入并单步执行,它将起作用!我认为我手动执行的延迟允许请求完成。

这是我的代码:

browser = Watir::Browser.new :phantomjs
browser.goto(map_page_url)
map_page = Nokogiri::HTML(browser.html)

#undefined method 'attributes' for nil:NilClass:
coord_url = map_page.at('p:contains("View as") a').attributes['href'].value

如何让它等到browser.goto(map_page_url) 完全返回后再继续?

【问题讨论】:

    标签: ruby-on-rails ruby phantomjs watir


    【解决方案1】:

    您不需要为此使用 nokogiri。

    browser.p(text: /View as/).link.when_present.attribute_value('href')
    

    【讨论】:

      猜你喜欢
      • 2021-02-16
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 2015-07-17
      • 2014-07-14
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多