【问题标题】:How to Continue when No such Element没有此类元素时如何继续
【发布时间】:2014-05-13 15:59:48
【问题描述】:

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

顺便说一句,我正在使用 ruby​​。

...response.rb:51:in `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

谢谢, 斯科特

【问题讨论】:

    标签: ruby selenium webdriver element


    【解决方案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
    

    【讨论】:

    • 谢谢理查德。我看到一些帖子讨论救援,但我不知道它在说什么。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2019-07-04
      • 2015-02-11
      • 1970-01-01
      相关资源
      最近更新 更多