加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Capybara,Poltergeist和Phantomjs并在身体上给

发布时间:2020-12-16 20:49:21 所属栏目:百科 来源:网络整理
导读:我是从phantomjs回来的空文件.我正在尝试使用Capybara和Poltergeist为Capybara设置phantomjs驱动程序. 我创建了一个模块如下,并将其包含在使用连接需求的文件中. require 'capybara/poltergeist' module Parser module JSParser include Capybara # Create a
我是从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,head和body标签,但在body标签中没有任何内容.

我做错了什么?观察网络流量,我得到主机的完整回复(在这种情况下为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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读