6

我创建了一个将Watir::Browserchrome 设置为浏览器的实例。

b = Watir::Browser.new :chrome    

如果我手动关闭警报,然后尝试使用刷新页面

b.refresh

抛出以下错误:

Selenium::WebDriver::Error::UnhandledAlertError: unexpected alert open
  (Session info: chrome=28.0.1500.95)
  (Driver info: chromedriver=2.2,platform=Windows NT 6.1 x86_64)
        from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/response.rb:51:in     assert_ok'
        from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/response.rb:15:in     initialize'
        from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/http/common.rb:59:in     new'
        from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/http/common.rb:59:in     create_response'
        from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/http/default.rb:66:in     request'
        from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/http/common.rb:40:in     call'
        from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/bridge.rb:634:in     raw_execute'
        from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/bridge.rb:612:in     execute'
        from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/remote/bridge.rb:210:in     refresh'
        from C:/ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.0/lib/selenium/webdriver/common/navigation.rb:38:in     refresh'
        from C:/ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.4/lib/watir-webdriver/browser.rb:201:in     refresh'

当我调用b.alert.exists?它时返回true

我尝试查看使用b.alert.initialize来重置警报的状态,但它引发了异常。

有没有办法完全重置浏览器对象的状态?

4

1 回答 1

7

我也遇到了这个问题:

但是我通过将它放在一个begin-rescue块中成功地摆脱了它。

方法如下:

  begin
    # put line of code which triggers alert on page
  rescue Selenium::WebDriver::Error::UnhandledAlertError
    @browser.alert.ok 
  end
于 2015-02-18T13:04:24.323 回答