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