如果超时,则等待元素而不抛出异常

时间:2021-03-11 19:44:41

I know of the method Element#wait_until_present(t), but if this method times out it throws a timeOut exception.

我知道方法元素#wait_until_present(t),但是如果这个方法超时,就会抛出一个超时异常。

Is there a method that just waits for t seconds and then returns true if the element became present or false otherwise?

是否有一种方法可以等待t秒,然后返回true,如果元素变成了present或false ?

I know it can be done with a simple begin..rescue..end statement, but I'm looking for something that doesn't use exceptions.

我知道只要一个简单的开始就可以做到。结束语句,但是我正在寻找不使用异常的东西。

3 个解决方案

#1


4  

You can write a short-hand rescue clause like this:

你可以这样写一个紧急救援条款:

element_present = browser.element.wait_until_present rescue false
puts "element not present" unless element_present

This does however result in a false value on any Exception at all and not just with TimeoutError. I still prefer to use it since if there's any Exception at all then it would be safer to assume that the element was not present.

然而,这会导致对任何异常的错误值,而不仅仅是时间恐怖。我仍然更喜欢使用它,因为如果有任何异常,那么更安全的假设是元素不存在。

#2


3  

Looks like there is no other method that will do what i'm looking for ,

看起来没有其他方法可以做我想要的,

so here is the simplest method to achieve this :

这是实现这一目标的最简单的方法:

#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

#3


1  

If you do not want an exception the below code can be handy:

如果您不想要异常,下面的代码可以很方便:

sleep 'your time here' eg: sleep 20 - this will wait for 20 secs.

睡“你的时间”例如:睡20秒——这要等20秒。

then check for your element now:

然后检查你的元素:

'your element'.exists? -this will return true/false

“你的元素.exists”呢?这将返回true / false

you will not get an exception this way.

你不会有这样的例外。

Another best way is to write your wait_for_load method based on your needs.

另一种最好的方法是根据需要编写wait_for_load方法。

#1


4  

You can write a short-hand rescue clause like this:

你可以这样写一个紧急救援条款:

element_present = browser.element.wait_until_present rescue false
puts "element not present" unless element_present

This does however result in a false value on any Exception at all and not just with TimeoutError. I still prefer to use it since if there's any Exception at all then it would be safer to assume that the element was not present.

然而,这会导致对任何异常的错误值,而不仅仅是时间恐怖。我仍然更喜欢使用它,因为如果有任何异常,那么更安全的假设是元素不存在。

#2


3  

Looks like there is no other method that will do what i'm looking for ,

看起来没有其他方法可以做我想要的,

so here is the simplest method to achieve this :

这是实现这一目标的最简单的方法:

#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

#3


1  

If you do not want an exception the below code can be handy:

如果您不想要异常,下面的代码可以很方便:

sleep 'your time here' eg: sleep 20 - this will wait for 20 secs.

睡“你的时间”例如:睡20秒——这要等20秒。

then check for your element now:

然后检查你的元素:

'your element'.exists? -this will return true/false

“你的元素.exists”呢?这将返回true / false

you will not get an exception this way.

你不会有这样的例外。

Another best way is to write your wait_for_load method based on your needs.

另一种最好的方法是根据需要编写wait_for_load方法。

相关文章