LoginSignup
23
13

More than 5 years have passed since last update.

Capybaraで「Request failed to reach server, check DNS and/or server status」のエラー

Posted at

現象

Capybaraでhttpsのサイトにvisit('')したときに、以下のエラーが発生する。

Request failed to reach server, check DNS and/or server status

/usr/local/lib/ruby/gems/2.1.0/gems/poltergeist-1.6.0/lib/capybara/poltergeist/browser.rb:323:in `command': Request failed to reach server, check DNS and/or server status (Capybara::Poltergeist::StatusFailError)
    from /usr/local/lib/ruby/gems/2.1.0/gems/poltergeist-1.6.0/lib/capybara/poltergeist/browser.rb:31:in `visit'
    from /usr/local/lib/ruby/gems/2.1.0/gems/poltergeist-1.6.0/lib/capybara/poltergeist/driver.rb:95:in `visit'
    from /usr/local/lib/ruby/gems/2.1.0/gems/capybara-2.4.4/lib/capybara/session.rb:227:in `visit'
    from /usr/local/lib/ruby/gems/2.1.0/gems/capybara-2.4.4/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
    from itunes.rb:24:in `login'
    from itunes.rb:61:in `<main>'

原因

SSLの通信にエラーが起きてるため。

The problem is most likely due to SSL certificate errors. If you start phantomjs with the --ignore-ssl-errors=yes option, it should proceed to load the page as it would if there were no SSL errors:
screen scraping - PhantomJS failing to open HTTPS site - Stack Overflow

対処法

以下のオプションを設定することで解決した。

phantomjs_options: ['--ignore-ssl-errors=yes']

上記オブションのみで解決しない場合は、--load-images=no, --ssl-protocol=anyのオプションも試してみるとよいとのこと。

Capybara.register_driver :poltergeist do |app|
  Capybara::Poltergeist::Driver.new(app, {
                    js_errors: false,
                    timeout: 1000,
                    phantomjs_options: [
                              '--load-images=no',
                              '--ignore-ssl-errors=yes',
                              '--ssl-protocol=any']})
end

ローカル環境では、上記の「phantomjs_options」を指定しなくても動作するが、CentOS上だとエラーになる。インストールしているphantomjsの違いなのか、SSL通信をする際の挙動が違うのか理解していないため、要調査。

参考

23
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
13