Reputation: 1
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
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
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
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
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