4

我正在使用 Capybara(Selenium 驱动程序)访问网站上的某些页面。我只需单击数组中的每个项目,然后单击后退按钮。它运行良好,但每次经过一些迭代后它都会中断。这是代码:

all(:xpath, '//table[@class="griglia_bordata"]//tr[td]/td/a[1]').each do |a|
  a_js_functions << a[:href]
end
a_js_functions.each do |js_for_model|
  puts js_for_model
  page.execute_script js_for_model
  find(:xpath, "//a[text()='Check availability']").click
  puts find(".testo_grande_blu_B").text
  puts "--------------------------------------------"
  find(:xpath, "//a[text()='Back']").click
end

我有输出:

javascript:selectModel('130254') 样式:RB2132 --------------------------------------- ----- javascript:selectModel('309257') 样式:RB2140 ---------------------------------- ---------- javascript:selectModel('68238') 样式:RB3016 ----------------------------- --------------- javascript:selectModel('68248') 样式:RB3025 ------------------------ -------------------- javascript:selectModel('68293') 样式:RB3026 -------------------- ------------------------- javascript:selectModel('68320') 样式:RB3044 -------------- ------------------------------ javascript:selectModel('68460') /usr/lib/ruby/1.9.1/net /protocol.rb:146:in rescue in rbuf_fill': Timeout::Error (Timeout::Error) from /usr/lib/ruby/1.9.1/net/protocol.rb:140:inrbuf_fill' 来自 /usr/lib/ruby/1.9.1/net/protocol.rb:122:in readuntil' from /usr/lib/ruby/1.9.1/net/protocol.rb:132:inreadline' 来自 /usr/lib/ruby/1.9.1/net/http.rb :2562:在read_status_line' from /usr/lib/ruby/1.9.1/net/http.rb:2551:in从 /usr/lib/ruby/1.9.1/net/http.rb:1319:in 中读取新的'从 /usr/lib/ruby/1.9.1/net/http.rb:1316:in 中block in transport_request' from /usr/lib/ruby/1.9.1/net/http.rb:1316:in捕获'transport_request' from /usr/lib/ruby/1.9.1/net/http.rb:1293:in来自/的请求' usr/lib/ruby/1.9.1/net/http.rb:1286:in block in request' from /usr/lib/ruby/1.9.1/net/http.rb:745:instart' from /usr/lib/ruby/1.9.1/net/http.rb:1284:in request' from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:82:in response_for' from /var/lib/ gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:38:in request' from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/common.rb:40:in call'来自/var/lib/gems/1.9.1/gems/selenium -webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:598:in raw_execute' from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:576:in execute' 来自 /var/lib/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/ webdriver/remote/bridge.rb:554:in find_elements_by' from /var/lib/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/common/search_context.rb:62:in find_elements' from /var/lib/gems/1.9.1/gems/capybara-1.1.2/lib/capybara/selenium/driver.rb:52:in find' from /var/lib/gems/1.9.1/gems/capybara-1.1.2/lib/capybara/node/finders.rb:158:in find_in_base' 来自 /var/lib/gems/1.9.1/gems/capybara-1.1.2/lib/capybara/node/finders.rb:137:in block in first' from /var/lib/gems/1.9.1/gems/capybara-1.1.2/lib/capybara/node/finders.rb:136:in each' 来自 /var/lib/gems/1.9.1/gems /capybara-1.1.2/lib/capybara/node/finders.rb:136:in first' from /var/lib/gems/1.9.1/gems/capybara-1.1.2/lib/capybara/node/finders.rb:27:in block in find' from /var/lib/gems/1.9.1/gems/capybara-1.1.2/lib/capybara/node/ base.rb:46:in wait_until' from /var/lib/gems/1.9.1/gems/capybara-1.1.2/lib/capybara/node/finders.rb:27:in find' from (eval):2:in find' from /var/lib/gems/1.9.1/gems/capybara-1.1.2/lib/capybara/dsl.rb:161:in find' from /home/biske/workspace/ScrapingGlasses/lib/luxottica.rb:39:in block in scrape' from /home/biske/workspace/ScrapingGlasses/lib/luxottica.rb:36:ineach' from /home/biske/workspace/ScrapingGlasses/ lib/luxottica.rb:36:in scrape' from /home/biske/workspace/ScrapingGlasses/lib/luxottica.rb:12:ingo' 来自 /home/biske/workspace/ScrapingGlasses/lib/luxottica.rb:48:in `'

4

2 回答 2

6

我之前看到过类似的痕迹,并且有两个潜在原因:

1) 像 FakeWeb 和 WebMock 这样的 gem 修改了 ruby​​ 的 http。尝试删除那些你可能用来模拟/阻止网络请求的宝石和任何类似的宝石。

2) 我在一个真正负载过重的系统中看到过这种超时会随机发生的情况。真正的问题不是测试,而是系统以及在其上运行的内容。可以更改 http 库使用的超时并继续测试。

为 Capybara 更新:(来自http://selenium.googlecode.com/svn/wiki/RubyBindings.wiki

Capybara.register_driver :selenium_extended_http_timeout do |app|
    client = Selenium::WebDriver::Remote::Http::Default.new
    client.timeout = 240
    Capybara::Selenium::Driver.new(app, 
                               :browser => :firefox, 
                               :http_client => client, 
                               :resynchronization_timeout => 60,
                               :resynchronize => true)                                                                   
end
Capybara.javascript_driver = :selenium_extended_http_timeout

您很有可能不需要重新同步的东西。

在这两种情况下,这都与 selenium 如何进行一些内部通信有关。JsonWire 协议。

于 2012-10-20T00:52:48.920 回答
0

我在使用 Fakeweb 和 VCR 时遇到了这个问题,但是在升级到 selenium-webdriver (2.27.0) 之后,超时就消失了

于 2012-12-08T16:00:50.620 回答