我知道Element#wait_until_present(t)方法,但是如果这个方法超时,它会抛出一个timeOut异常。
是否有一个方法只等待t秒,如果元素出现,则返回true,否则返回false?
我知道这可以通过一个简单的begin..rescue..end语句来完成,但是我正在寻找不使用异常的东西。
发布于 2013-01-08 00:33:17
您可以像这样编写一个速记rescue子句:
element_present = browser.element.wait_until_present rescue false
puts "element not present" unless element_present但是,这确实会在任何Exception上产生false值,而不仅仅是TimeoutError。我仍然更喜欢使用它,因为如果有任何Exception,那么假设该元素不存在会更安全。
发布于 2013-01-07 17:20:42
看起来没有其他方法可以达到我想要的效果,
所以这里是实现这一点的最简单的方法:
#check method for Element#wait_until_present(t)
def check_if_present(element,t)
raise ArgumentError, 't must be a number ' unless t.is_a? Numeric
begin
element.wait_until_present(t)
true
rescue Watir::Wait::TimeoutError
false
rescue
raise "Something wrong with the element"
end
end发布于 2013-01-07 17:20:30
如果你不想要一个异常,下面的代码会很方便:
在这里“享受你的时光”例如:睡眠20 --这将等待20秒。
然后现在检查您的元素:
‘'your’..exists?-this将返回true/false
您不会通过这种方式获得异常。
另一种最好的方法是根据需要编写wait_for_load方法。
https://stackoverflow.com/questions/14192073
复制相似问题