Capybara,Poltergeist和Phantomjs,并在身体中做出空洞的反应[英] Capybara, Poltergeist and Phantomjs and giving an empty response in body

问题描述

我从phantomjs恢复了空的文件.我正在尝试使用Capybara和Poltergeist为Capybara设置Phantomjs驱动程序.

我创建了一个模块,如下所示,并将其包含在使用需要连接的文件中.

require 'capybara/poltergeist'

  module Parser
    module JSParser
      include Capybara

      # Create a new PhantomJS session in Capybara
      def new_session
        # Register PhantomJS (aka poltergeist) as the driver to use
        Capybara.register_driver :poltergeist do |app|
          Capybara::Poltergeist::Driver.new(app, :debug => true)
        end

        # Use XPath as the default selector for the find method
        Capybara.default_selector = :xpath
        Capybara.javascript_driver = :poltergeist
        Capybara.current_driver = :poltergeist
        # Start up a new thread
        @session = Capybara::Session.new(:poltergeist)

        # Report using a particular user agent
        @session.driver.headers = { 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X)' }

        # Return the driver's session
        @session
      end

      # Returns the current session's page
      def html
        @session.html
      end

    end
  end

然后,加载页面如下:

class Loader
  include Parser::JSParser

  def load_page
    new_session
    visit "http://www.smashingmagazine.com"
    #let phantomjs take its time
    sleep 5
    puts "html=#{html}"  
  end
end

然后,最后调用load_page

Loader.new.load_page

这是Poltergeist的调试响应

poltergeist [1364758785355] state default -> loading
{"response"=>true}
{"name"=>"visit", "args"=>["http://www.smashingmagazine.com"]}
poltergeist [1364758794574] state loading -> default
{"response"=>{"status"=>"success"}}
{"name"=>"body", "args"=>[]}
{"response"=>"<html><head></head><body></body></html>"}

您可以看到,响应只是一个空白的文档,只有HTML,头部和身体标签,但在身体标签中没有任何内容.

我在做什么?观察网络流量,我从主机那里得到了完整的响应(在这种情况下为SmashingMagazine.com).回应回来后,我不知道发生了什么.有时,phantomjs也崩溃了,在其他情况下,它与空体一起经过.这是phantomjs崩溃

时在stderr上打印的最后一行
PhantomJS client died while processing {"name":"visit","args":["http://www.smashingmagazine.com"]}

推荐答案

我也有类似的问题.但是以下选项设置:phantomjs_options,帮助我解决了问题.

  Capybara.register_driver :poltergeist do |app|
    Capybara::Poltergeist::Driver.new(app,
                                      :phantomjs_options => ['--debug=no', '--load-images=no', '--ignore-ssl-errors=yes', '--ssl-protocol=TLSv1'], :debug => false)
  end

其他推荐答案

访问此网站时听起来像是phantomjs中的错误.我建议尝试使用仅 phantomjs(不是Poltergeist或Capybara)加载网站,以查看是否有效.如果也有麻烦,请报告针对phantomjs的错误.

您可以在此处阅读有关phantomjs的信息:/wiki/Quick-Start

其他推荐答案

使用此html puts "html=#{page.html}"

本文地址:https://www.itbaoku.cn/post/1739851.html