Wait until a specific item disappears

Using Ruby, we can wait for a specific item by doing the following:

wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until { driver.find_element(:class, 'gritter-item') }

but if I want some element to disappear from the DOM, I write a method like:

  def disappear_element
    begin
      driver.find_element(:class, 'gritter-item')
    rescue Selenium::WebDriver::Error::NoSuchElementError
      true
    else
      false
    end
  end

and is called like this:

wait.until { disappear_element }

Thus, I could achieve the absence of an element. Is there a better way in Ruby to achieve the same?

+4
source share
1 answer

You can write disappear_elementas follows (using find_elementsinstead find_element):

def disappear_element
  driver.find_elements(:class, 'gritter-item').size == 0
end
+3
source

All Articles