Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

ruby on rails - Capybara, Poltergeist and Phantomjs and giving an empty response in body

I am getting am empty document back from phantomjs. I am trying to use Capybara and Poltergeist for setting up the phantomjs driver for Capybara.

I created a module as follows and included it in the file that uses needs to connect.

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

Then, loading the page as follows:

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

Then, finally, calling the load_page

Loader.new.load_page

Here is the debug response from 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>"}

As you can see, the response is just a blank document with only the html, head and body tags but nothing in the body tag.

What wrong am I doing? Observing network traffic, I am getting the full response back from the host (smashingmagazine.com in this case). Its after the response comes back that I don't know what is happening. Sometimes phantomjs is also crashing and on other occasions, it goes through with the empty body. Here is the last line that is printed on STDERR when phantomjs crashes

PhantomJS client died while processing {"name":"visit","args":["http://www.smashingmagazine.com"]}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I also had the similar issue. But the below option setting :phantomjs_options, helped me to solve the issue.

  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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...