如何解决此错误? Net :: ReadTimeout(Net :: ReadTimeout)

时间:2019-08-25 12:08:14

标签: ruby selenium capybara

实际上,我意识到它对于某些类别(例如

step %{I go to "https://newyork.craigslist.org/search/spa?s=#{emails}"}

但不适用于其他人,例如

 # step %{I go to "https://newyork.craigslist.org/search/fbh?s=#{emails}"}

我的函数运行了好几天,然后突然开始发出以下错误: Net :: ReadTimeout(Net :: ReadTimeout),恰好在i = 120时。

有什么我可以解决的吗?

Given(/^I go to "([^"]*)"?/) do |url|
  visit(url)
end

Given("I save all emails") do
  emails = 0
  i = 119
  until emails >= 500
      until i == 120
          fetch_emails(i, emails)
          i += 1
      end
      click_next_button
      emails += 120
      puts emails
      i = 1
      puts i
    end
end

def fetch_emails(i, emails)
      find(:xpath, "(//a[@class='result-title hdrlnk'])[#{i}]").click
      if Capybara.has_xpath?("//button[@class='reply-button js-only']")
        find(:xpath, "//button[@class='reply-button js-only']").click
        sleep(1)
        if Capybara.has_xpath?("//p[@class='reply-email-address']")
          # puts find(:xpath, "//p[@class='reply-email-address']//a").text
          open('RESULTS.csv', 'a') do |f|
            f << find(:xpath, "//p[@class='reply-email-address']//a").text + "\n"
          end
        end
      end
      # step %{I go to "https://newyork.craigslist.org/search/fbh?s=#{emails}"}
       step %{I go to "https://newyork.craigslist.org/search/rfh?s=#{emails}"}
      # step %{I go to "https://newyork.craigslist.org/search/lab?s=#{emails}"}
#      step %{I go to "https://newyork.craigslist.org/search/spa?s=#{emails}"}
      # step %{I go to "https://newyork.craigslist.org/search/trd?s=#{emails}"}
end

def click_next_button
    first(".next").click
    sleep(2)
end

1 个答案:

答案 0 :(得分:0)

如果您的Chrome升级到最新版本,请使用以下功能

capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: {
      args: %w[
        headless disable-gpu no-sandbox
        --window-size=1980,1080 --enable-features=NetworkService,NetworkServiceInProcess
      ]
    }
  )


Capybara::Selenium::Driver.new app, browser: :chrome, desired_capabilities: capabilities