I am not a Ruby expert and can make some syntax errors, but you can get a general idea:
if @driver.find_elements(:link, "Save").size() > 0
This code does not throw a NoSuchElementException
But this method will freeze for a while if you have implicitlyWait greater than zero and there are no elements on the page. The second problem: if the element exists on the page but is not displayed, you will get true .
So the workaround is trying to create a method:
def is_element_present(how, what) @driver.manage.timeouts.implicit_wait = 0 result = @driver.find_elements(how, what).size() > 0 if result result = @driver.find_element(how, what).displayed? end @driver.manage.timeouts.implicit_wait = 30 return result end
Igor Khrol
source share