水豚,Poltergeist和Phantomjs,并在体内给出空洞的响应 [英] Capybara, Poltergeist and Phantomjs and giving an empty response in body
问题描述
我正在从phantomjs返回空文档.我正在尝试使用Capybara和Poltergeist为Capybara设置phantomjs驱动程序.
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
然后,最后,调用load_page
Then, finally, calling the load_page
Loader.new.load_page
这是来自poltergeist的调试响应
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>"}
如您所见,响应只是一个空白文档,仅包含html,head和body标签,而body标签中则什么也没有.
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.
我在做什么错?观察网络流量,我从主机(在本例中为smashingmagazine.com)处获得了完整的响应.回复回来后,我不知道发生了什么.有时phantomjs也崩溃了,在其他情况下,它伴随着空荡荡的身体.这是phantomjs崩溃时STDERR上打印的最后一行
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"]}
推荐答案
我也遇到了类似的问题.但是下面的选项设置:phantomjs_options
帮助我解决了该问题.
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
这篇关于水豚,Poltergeist和Phantomjs,并在体内给出空洞的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!