Capybara, Poltergeist and Phantomjs and giving an empty response in body

一曲冷凌霜 提交于 2019-11-28 05:49:18

问题


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"]}

回答1:


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



回答2:


It sounds like a bug in PhantomJS when visiting this website. I suggest trying to load the website using only PhantomJS (not Poltergeist or Capybara) to see whether that works. If it also has trouble, report a bug against PhantomJS.

You can read about PhantomJS here: https://github.com/ariya/phantomjs/wiki/Quick-Start




回答3:


Get html with this instead puts "html=#{page.html}"



来源:https://stackoverflow.com/questions/15733827/capybara-poltergeist-and-phantomjs-and-giving-an-empty-response-in-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!