【问题标题】:rescue not catch error 'Selenium::WebDriver::Error'救援未捕获错误“Selenium::WebDriver::Error”
【发布时间】:2015-07-02 11:21:05
【问题描述】:

当我的代码遇到select_to_city(to)

我猜它会被Selenium::WebDriver::Error打破

但它并没有停止救援为什么?

class Tiger123 < ClassTemplate
    def form_action(from, to, flight_date)
      begin
        select_to_city(to)
        select_depart_date(flight_date)
      rescue Selenium::WebDriver::Error => e
        binding.pry
      rescue Exception => e
        binding.pry
      end
    end


def select_to_city(to)
  @driver.find_element(:id, "selDestPicker").click
  @driver.find_element(:id, to).click    
end

更新

最后我在 select_to_city 函数中添加了rescue

它确实奏效了。我不明白为什么它没有在form_action 方法中进行救援

def select_to_city(to)
begin
@driver.find_element(:id, "selDestPicker").click
@driver.find_element(:id, to).click          
rescue Exception => e
  binding.pry
end
end

【问题讨论】:

  • 你猜它会坏掉吗?可以?如果它确实抛出了什么样的异常,因为无论如何它都会被Exception救出,除非它实际上并没有引发异常。
  • @engineersmnky 我确信救援根本不起作用。因为我停在 select_to_city(to) 之前的第一个断点,然后是continue,然后它没有停在select_depart_date(flight_date) 之后的breakpoint

标签: ruby selenium-webdriver rescue


【解决方案1】:

拯救Selenium::WebDriver::Error 不起作用,因为异常不属于该类。 Selenium::WebDriver::Error 只是包含不同错误类型的模块。

在一般Exception的救援过程中,可以通过ancestors方法看到异常类及其祖先:

rescue Exception => e
  p e.class.ancestors
  #=> [Selenium::WebDriver::Error::NoSuchElementError, Selenium::WebDriver::Error::WebDriverError, StandardError, Exception, Object, Kernel, BasicObject]
end

数组中的第一项是异常的类,恰好是 Selenium 中的特定错误。下一项,Selenium::WebDriver::Error::WebDriverError,是所有 Selenium 异常都继承自的父类。您要拯救的正是这个父类:

def form_action(from, to, flight_date)
  begin
    select_to_city(to)
    select_depart_date(flight_date)
  rescue Selenium::WebDriver::Error::WebDriverError => e
    binding.pry
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多