John Hammink
John Hammink

Reputation: 1

Unable to find method for detecting if element exists on a page

I'm very new to Watir.

I have a bit of Ruby/Watir code that is supposed to detect if an element, exists, and if so, click it, if not, click a different element. Both elements, show up, every time. Unfortunately nothing I've tried works.

if browser.contains_text("/media/images/icons/reviewertools/editreview.jpg")
then browser.image(:src => "/media/images/icons/reviewertools/editreview.jpg").click
else browser.image(:src => "/media/images/icons/reviewertools/savereview.jpg").click
end

This eventually fails with "Unable to locate element, using {:src=>"/media/images/icons/reviewertools/savereview.jpg"} (Watir::Exception::UnknownObjectException)"

It should have clicked /editreview.jpg, which was visible.

I have also tried:

if browser.image("/media/images/icons/reviewertools/savereview.jpg").exists
then browser.image(:src => "/media/images/icons/reviewertools/savereview.jpg").click

as well as:

if browser.image("/media/images/icons/reviewertools/savereview.jpg").exists?

Note that NONE of these cases detect the element, or failing to do that, execute the else clause.

Please, if you respond, provide specific code examples for your suggestions.

Upvotes: 0

Views: 3423

Answers (4)

Bret Pettichord
Bret Pettichord

Reputation: 455

You need to use the same syntax with both the click and the exists? methods. This has also been pointed out by Kat and Chuck and Kinofrost.

if browser.image(:src => "/media/images/icons/reviewertools/editreview.jpg").exists?
then browser.image(:src => "/media/images/icons/reviewertools/editreview.jpg").click
else browser.image(:src => "/media/images/icons/reviewertools/savereview.jpg").click
end

Upvotes: 2

Chuck van der Linden
Chuck van der Linden

Reputation: 6660

Presuming that this thing really is an 'image' as defined by it's HTML tag, your attempt to identify if the object is there is failing because

  1. you are not using the right method, in Ruby, methods that return a bool end in a question mark
  2. it's not text,

and in the other attempt you provided only a 'what' and not a how, and still used the wrong method.

try this

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

Otherwise have a good look at the HTML, are you actually looking at a 'button' perhaps?

Upvotes: 1

kinofrost
kinofrost

Reputation: 768

I'd recommend checking the DOM to double check the src for your image matches what you're putting. Press F12 in IE8, or use whatever tool is relevant to your browser. You could try using IRB to connect to the browser and try and find the image.

If these fail then I'd try locating the image another way. If the image is in a form this can cause problems and you'll have to locate the form before the image.

Or try another way to locate it, just to make sure that it's possible.

browser.image(:index => 3).click
browser.image(:id => 'an_image').click
browser.div(:id => 'image_container').image(:index => 2).click

You can use this link to see what ways you can identify an image, and don't forget that you can use more than one identifier at a time, eg. (:class => /regexofaclass/, :index => 2)

There's nothing wrong with your code as it is (apart from the ? at the end of "exists", and the last line which doesn't contain what you're looking for).

Upvotes: 1

katmoon
katmoon

Reputation: 1024

  1. There are only methods "exist?" and "exists?". So "exist" won't work. Consult here.
  2. Can you try to identify existence of a different element, such as a link. Does it work for you? There shouldn be no exception. Watir needs to return False in this case.

Upvotes: 2

Related Questions