I am using the Ruby interpretation of Webdriver. Now the webpage I am looking at may or may not contain a certain element. If it doesn’t I would like to look for another element. However, whenever Webdriver can’t find an element it throws an exception.
My question is, can I check to see if the element even exists on the page without littering my code with begin/rescue blocks ?
Any help is greatly appreciated.
The short answer is no. To check if element exists, you have to rescue errors.
find_elementalways throwNoSuchElementErrorif element was not found. You can for example take a look at implementation of#exists?method in Watir-WebDriver.Still, you can use
find_elementsmethod which will return empty array if not elements were found.