Could not find a method to determine if an item exists on the page

I am very new to Vatira.

I have some Ruby / Watir code that should determine if an element exists, and if so, click it, if not, click another element. Both elements appear every time. Unfortunately, nothing I 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 

As a result, this fails: "Unable to find element using {: src =>" /media/images/icons/reviewertools/savereview.jpg "} (Watir :: Exception :: UnknownObjectException)"

He had to click /editreview.jpg, which was visible.

I also tried:

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

and:

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

Note that NONE, in these cases, either detects an item or fails to do so, executes an else clause.

Please respond with specific examples of code for your suggestions.

0
source share
4 answers
  • There are only "exist?" Methods. and "exists?" So "exists" will not work. Contact here .
  • Can you try to determine the existence of another element, such as a link. Does this work for you? There should be no exceptions. In this case, Watari must return False.
+2
source

Do you need to use the same syntax with both a click and an existing one? methods. This was also indicated 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 
+2
source

I would recommend checking the DOM to double check that the src for your image matches what you are setting. Press F12 in IE8 or use any tool that matches your browser. You can try using IRB to connect to the browser and try and find the image.

If they do not work, I will try to find the image in another way. If the image is in shape, this can cause problems and you will need to find the shape in front of the image.

Or try another way to find it, just to make sure it is 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 find out how you can identify the image, and do not forget that you can use more than one identifier at a time, for example. (: class => / regexofaclass / ,: index => 2)

There is nothing wrong with the code (except for the "at the end" exists, and the last line that does not contain what you are looking for).

+1
source

Assuming this thing is really an β€œimage” defined by an HTML tag, your attempt to determine if an object exists is missing because

  • you are not using the correct method, in Ruby, methods that return the end of a bool to a question mark
  • this is not text

and in another attempt, you provided only β€œwhat” and not how, and still used the wrong method.

try it

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

Otherwise, take a good look at HTML, are you really looking at 'button', maybe?

+1
source

All Articles