Capybara, Poltergeist and Phantomjs and giving an empty response in body
Asked Answered
A

3

3

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"]}
Anesthetist answered 31/3, 2013 at 19:54 Comment(0)
S
5

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
Surrebutter answered 11/11, 2014 at 11:30 Comment(3)
Thanks for this. The '--ssl-protocol=TLSv1' argument solved a similar empty response issue for me.Asbestos
After hours of debugging a rarely used feature that relies on casper.js and phantomjs, I stumbled on this answer. We had upgraded the security on our reverse proxy a few months ago ....facepalm. I can't believe phantomjs just silently fails when this happens. Thank you!!!Hogarth
I've been pulling my hair off for the past 8 hours, thanks a lot for these options that saved my day. cheers !Bisitun
F
1

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

Furcula answered 2/4, 2013 at 11:53 Comment(0)
C
0

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

Caruso answered 20/4, 2013 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.