How to detect if an element exists in Watir

Rijksband picture Rijksband · Mar 18, 2011 · Viewed 21.8k times · Source

I'm relatively new to Watir but can find no good documentation (examples) regarding how to check if an element exists. There are the API specs, of course, but these make precious little sense to me if I don't find an example.

I've tried both combinations but nothing seems to work... if browser.image (:src "/media/images/icons/reviewertools/editreview.jpg").exists then... if browser.image (:src "/media/images/icons/reviewertools/editreview.jpg").exists? then...

If anyone has a concrete suggestion as per how to implement this, please help! Thanks!

Answer

katmoon picture katmoon · Mar 19, 2011

It seems you are missing a comma between parameters.

Should be

if browser.image(:src, "/media/images/icons/reviewertools/editreview.jpg").exists?

Also you can find this page useful in future to know what attributes are supported.