简体   繁体   English

如何正确调试Capybara / Poltergeist?

[英]How to properly debug with Capybara/Poltergeist?

I am playing around with capybara/poltergeist perfect duo, but I am having trouble to properly debugging. 我正在玩capybara / poltergeist完美二人组,但我无法正常调试。 I was testing a simple script: 我正在测试一个简单的脚本:

logger = Logger.new "./log/who-scored-com.log"
Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, js_errors: false,
                                         debug: true,
                                         logger: logger)
end
browser = Capybara.current_session
browser.visit 'https://www.whoscored.com/LiveScores'
browser.save_page 'page.html'

I am expecting that the script grap the page normally and saves it, but the page is empty and this is returned: 我希望脚本正常抓取页面并保存它,但页面为空并返回:

`Capybara::Poltergeist::StatusFailError: Capybara::Poltergeist::StatusFailError
    from /home/vagrant/local/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/poltergeist-1.9.0/lib/capybara/poltergeist/browser.rb:351:in `command'
    from /home/vagrant/local/ruby-2.3.0/lib/ruby/gems/2.3.0/gems/poltergeist-1.9.0/lib/capybara/poltergeist/browser.rb:34:in `visit'`

Now, this don't give me nothing about this error. 现在,这不会给我这个错误。 I catch the exception and print it and it gives me: 我抓住了异常并打印出来,它给了我:

"Request to 'https://www.whoscored.com/LiveScores' failed to reach server, check DNS and/or server status"

Even if I have no idea why the address do not respond for capybara (and any hint would be appreciate :) ) I don't understand why the :debug options used in configuration doesn't seem to give me no info 即使我不知道为什么地址不响应capybara(任何提示都会欣赏:))我不明白为什么:debug配置中使用的:debug选项似乎没有给我任何信息

You have a couple of issues 你有几个问题

  1. the poltergeist logger option is defined as '(Object responding to puts) - The ruby 2.3.0 standard library Logger object doesnt respond to puts so it's not valid. poltergeist logger选项定义为'(对象响应放置) - ruby​​ 2.3.0标准库Logger对象不响应puts,因此它无效。

  2. You're example doesn't have Capybara.current_driver = :poltergeist so I'm not sure if it is actually using the driver you're configuring there or a previously defined one (I would have expected an error on the Logger object if it was) 你的例子没有Capybara.current_driver = :poltergeist所以我不确定它是否实际上是使用你在那里配置的驱动程序或者之前定义的驱动程序(如果它在Logger对象上,我会预料到它会出错)为)

  3. debug: true will add debugging for poltergeist to the log, but there is also debugging info from phantomjs. debug: true会将poltergeist的调试添加到日志中,但是还有来自phantomjs的调试信息。 That is generated by passing phantomjs_options: ['--debug=true'], phantomjs_logger: <an IO object - again not a Logger object> to the driver 这是通过将phantomjs_options: ['--debug=true'], phantomjs_logger: <an IO object - again not a Logger object>传递给驱动程序生成的

  4. The error you're actually hitting is the connection being refused due to not being able to negotiate an ssl protocol - to fix it add the required ssl protocol as a phantomjs option - `phantomjs_options: ['--ssl-protocol=TLSv1.2'] 您实际遇到的错误是由于无法协商ssl协议而导致连接被拒绝 - 修复它会将所需的ssl协议添加为phantomjs选项 - “phantomjs_options:[ - --ssl-protocol = TLSv1.2 “]

I'd use something like the following in a standalone ruby script, adjust driver_options to suit your taste. 我会在独立的ruby脚本中使用类似下面的内容,调整driver_options以满足您的口味。

require 'capybara'
require 'capybara/poltergeist'

class NilLogger
  def puts * ; end
end

def setup_session
  driver_options = { js_errors: false,
                     logger: NilLogger.new,
                     phantomjs_logger: STDOUT,
                     phantomjs_options: ['--debug=true'],
                     debug: false  }
  Capybara.configure do |conf|
    conf.run_server = false
    conf.register_driver :poltergeist do |app|
      Capybara::Poltergeist::Driver.new app, driver_options
    end
    conf.current_driver = :poltergeist
  end
  Capybara.current_session
end

browser = setup_session()
browser.visit 'https://www.whoscored.com/LiveScores'
browser.save_page 'page.html'

Have you tried 你有没有尝试过

Capybara.register_driver :poltergeist_debug do |app|
 Capybara::Poltergeist::Driver.new(app, :inspector => true)
end

Capybara.javascript_driver = :poltergeist_debug

as shown here ? 如图所示这里

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

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