没有这样的元素时如何继续

时间:2014-05-13 15:59:49

标签: ruby selenium webdriver element

我想验证一个对象是否存在。在下面的示例中,实际元素:id是" all-tab",我将其设置为" all2-tab"查看是否会显示失败通知。但是,我收到一个No元素错误,脚本停止。

BTW我正在使用红宝石。

... response.rb:51:在`assert_ok':无法找到id为== alll-tab的元素(Selenium :: WebDriver :: Error :: NoSuchElementError)

#View All Events Tab Exists
option = driver.find_element(:id,"all2-tab").displayed? #exists? 
if option == true
  puts"View All Events Tab Exists: PASS"
else
  puts"!!FAILED View All Events Tab Does not Exists"
end

谢谢, 斯科特

2 个答案:

答案 0 :(得分:1)

您可以使用beginrescue

begin
    option = driver.find_element(:id,"all2-tab").displayed? #exists? 
    if option == true
      puts"View All Events Tab Exists: PASS"
    else
      puts"!!FAILED View All Events Tab Does not Exists"
    end
rescue
    puts"Element does not exist"
end

答案 1 :(得分:0)

将代码包装在异常处理程序中,例如

begin
  # something which might raise an exception
rescue ExceptionClass => some_variable
end