sat*_*sat 3 ruby-on-rails capybara phantomjs poltergeist
我是从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
Run Code Online (Sandbox Code Playgroud)
然后,按如下方式加载页面:
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
Run Code Online (Sandbox Code Playgroud)
然后,最后,调用load_page
Loader.new.load_page
Run Code Online (Sandbox Code Playgroud)
这是来自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>"}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,响应只是一个空白文档,只有html,head和body标签,但在body标签中没有任何内容.
我做错了什么?观察网络流量,我收到主机的完整回复(在这种情况下为smashingmagazine.com).在回复之后,我不知道发生了什么.有时phantomjs也会崩溃,而在其他场合,它会通过空体.这是phantomjs崩溃时在STDERR上打印的最后一行
PhantomJS client died while processing {"name":"visit","args":["http://www.smashingmagazine.com"]}
Run Code Online (Sandbox Code Playgroud)
我也有类似的问题.但是下面的选项设置:phantomjs_options帮助我解决了这个问题.
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4684 次 |
| 最近记录: |