title: "Selenium::WebDriver::Error::UnhandledAlertError: unexpected alert open" despite using dismiss_confirm
body: <p>This test fails:</p>
  
  <pre><code>describe 'a confirm modal', :js, driver: :chrome do
    it do
      visit '/test/confirm'
      dismiss_confirm do
        click_link 'Cancel'
      end
    end
  end
  </code></pre>
  
  <p>with a <code>Selenium::WebDriver::Error::UnhandledAlertError: unexpected alert open</code> error.</p>
  
  <p>It fails immediately with this error as soon as it gets to the <code>click_link 'Cancel'</code> line:</p>
  
  <pre><code>An error occurred in an after hook
    Selenium::WebDriver::Error::UnhandledAlertError: unexpected alert open: {Alert text : Are you sure?}
    (Session info: chrome=53.0.2785.101)
    (Driver info: chromedriver=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a),platform=Linux 4.4.0-42-generic x86_64) (WARNING: The server did not provide any stacktrace information)
  Command duration or timeout: 1 milliseconds: null
  Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
  System info: host: '64c23d07d03c', ip: '172.18.0.6', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-42-generic', java.version: '1.8.0_03-Ubuntu'
  Driver info: org.openqa.selenium.chrome.ChromeDriver
  Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={chromedriverVersion=2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a), userDataDir=/tmp/.com.google.Chrome.nJ0sj0}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=53.0.2785.101, platform=LINUX, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
  Session ID: 12b4869246e45555e3456370e74ad536 (org.openqa.selenium.UnhandledAlertException)
    occurred at [remote server] sun.reflect.GeneratedConstructorAccessor31():-1:in `newInstance'
  </code></pre>
  
  <p>I can see in the Chrome window that it is still waiting for me to press OK or Cancel.</p>
  
  <p><strong>Why is this failing? How do I get it to dismiss the confirm instead of erroring out?</strong></p>
  
  <p>When I step through the code with <code>byebug</code>, I can see that <code>dismiss_modal</code> doesn't even <em>get</em> to the part where it tries to find a modal and dismiss. It errors out <em>immediately</em> when it hits the <code>click_link</code>, which I take it is not supposed to happen (otherwise <code>dismiss_modal</code> would have a <code>rescue</code> block for that):</p>
  
  <pre><code>[229, 238] in /usr/local/bundle/gems/capybara-2.10.1/lib/capybara/selenium/driver.rb
     229:     modal.accept
     230:     message
     231:   end
     232: 
     233:   def dismiss_modal(type, options={}, &amp;blk)
  =&gt; 234:     yield if block_given?
     235:     modal = find_modal(options)
     236:     message = modal.text
     237:     modal.dismiss
     238:     message
  (byebug) s
  
  [3, 12] in /app/spec/features/testing_basics_spec.rb
      3: describe 'a confirm modal', :js, driver: :chrome do
      4:   it do
      5:     visit '/test/confirm'
      6:     byebug
      7:     dismiss_confirm do
  =&gt;  8:       click_link 'Cancel'
      9:     end
  </code></pre>
  
  <p>The selenium server is running in a docker container from the <code>selenium/standalone-chrome-debug</code> image.</p>
  
  <p>The driver is configured with:</p>
  
  <pre><code>Capybara.register_driver :chrome do |app|
    Capybara::Selenium::Driver.new(
      app,
      browser:                :remote,
      url:                    "http://#{ENV['CHROME_WEBDRIVER_HOST']}:4444/wd/hub",
      desired_capabilities:   :chrome
    )
  end
  </code></pre>
  
  <p>It's using current versions of the gems (as far as I know):</p>
  
  <pre><code>  * capybara (2.10.1)
    * selenium-webdriver (2.53.4)
  </code></pre>
  
  <p>This is the Rails template it is interacting with (<code>app/views/confirm.html.haml</code>):</p>
  
  <p><code>
  = link_to 'Cancel', '#', :confirm =&gt; 'Are you sure?'
  </code></p>
  
  <p><strong>Update</strong>: Thanks to @TomWalpole and @SaurabhGaur for suggesting this could be due using an older version of chromedriver. </p>
  
  <p>It looks like I hadn't updated my <code>selenium/standalone-chrome-debug</code> image in a few weeks (though I'm surprised that 5 weeks is so old that it would cause a problem).</p>
  
  <p>Before updating...</p>
  
  <pre><code>selenium/standalone-chrome-debug latest 84d9c94a042a 5 weeks ago         844.8 MB
  
  root@65a9cd48f684:/# chromedriver --version                                                                                                                                                                
  ChromeDriver 2.21.371461 (633e689b520b25f3e264a2ede6b74ccc23cb636a)
  </code></pre>
  
  <p>After updating...</p>
  
  <pre><code>selenium/standalone-chrome-debug latest df941c594b0a 2 days ago          865.3 MB
  
  root@6800a351c182:/# chromedriver --version                  
  ChromeDriver 2.24.417424 (c5c5ea873213ee72e3d0929b47482681555340c3)
  </code></pre>
  
  <p>And it works!</p>