Using If Statement to compare screenshots

Asked by Pat S

I am trying to run a script that will plug information into a website and then the website will give 1 of 2 values which is either a green box or a red box.
I can automate up to the point of using an If statement to see which value has been returned.

I have tried

if (#Region.png).inside.exists(#Greenbox.png):
if (#Region.png).inside.exists(#Greenbox.png, 5)
if exists(#Greenbox.png):
if exists(#Greenbox.png,5):
if (#Region.png).inside.find.(#Greenbox.png):
and
if find(#Greenbox.png):

and all have returned errors. Am I wording this wrong or is it not possible to use a screenshot in your if statement?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Pat S
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

ok, the script is at the point, that you are seeing the green/red box on the screen.

try the following (supposing, that only one of these boxes is visible on the screen):

if exists(image-of-green-box, 0): # don't wait, just one try
   popup("found green")
elif exists(image-of-red-box, 0):
   popup("found green")
else:
   popup("neither green nor red")
   exit()
hover(getLastMatch) # mouse would be over center of red or green box whichever

if you have to wait, that either of these boxes comes up, the best way in the moment would be a loop (since on windows observe() does not work, on Mac would be a choice):

maxLoop = 10 # trying max 10 times
nLoop = 0
rgFound = False
while nLoop < maxLoop:
   if exists(image-of-green-box, 0): # don't wait, just one try
      rgFound = True
      popup("found green")
      break
   elif exists(image-of-red-box, 0):
      rgFound = True
      popup("found green")
      break
   else:
      wait(1) # wait 1 second
      nLoop += 1

if not rgFound:
   popup("neither green nor red")
   exit()

hover(getLastMatch) # mouse would be over center of red or green box whichever

Revision history for this message
Pat S (patricksurls) said :
#2

Thank you for the quick reply. That works like a charm.