Multiple if else Problem...

Asked by Vaishali

The below coding works fine till 2nd elif.. bt when it moves to 3rd elif its not able to identify the image..
if exists("Wl.png"):
          mouseMove("Wl.png")
          click("1390901431169-1.png")
 elif exists("1391427379709.png"):
          mouseMove("1391427379709.png")
          click("1390901695089.png")
 elif exists("1391402658828.png"):
          mouseMove("1391402658828.png")
          click("4.png")
 elif exists("1390903477117.png"):
          mouseMove("1390903477117.png")
          click("1390903521525.png")
 elif exists("1391424371172.png"):
          mouseMove("1391424371172.png")
          click("1391424719725.png")

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Test (c4456517) said :
#1

Ensure that the environment it's in doesn't detect any other instances of the image. Or you might want to try recapturing the image.

It would help to know what images you are exactly dealing with.

Revision history for this message
Vaishali (vaishoo-vasu) said :
#2

Ya checked for other matching images using match preview.. and the problem exists in 3rd elif irrespective of image.. when the same image is placed at 2nd elif it works fine...

Revision history for this message
Test (c4456517) said :
#3

So what is the exact error you're getting?

Revision history for this message
Test (c4456517) said :
#4

Oh, and try reducing the threshold of similarity for the image under the Matching preview tab in the Pattern Settings (when you click on the image in the IDE).

Revision history for this message
Vaishali (vaishoo-vasu) said :
#5

The IDE doesn't not check the 3rd elif statement it just goes to the statement below 3rd elif and start executing it.. even though the condition is not satisfied

Revision history for this message
Vaishali (vaishoo-vasu) said :
#6

Ya tried match preview.. Every image is perfect.. bt still the problem exist in 3rd elif..

Revision history for this message
Test (c4456517) said :
#7

That's odd, you don't receive any error message or anything?

Because if an image can't be found a FindFailed error should be displayed. What you mean is that:

elif exists("1390903477117.png"):
          mouseMove("1390903477117.png")
          click("1390903521525.png")

This is the 3rd elif statement. It skips the check to see whether the image exists and instead goes to the mouseMove() function?

Might want to check your indenting in the IDE. If it's not that, sorry but I have no idea what's wrong, might have to wait for RaiMan.

Revision history for this message
Vaishali (vaishoo-vasu) said :
#8

ya... it skips to check 3rd elif statement and moves to mouseMove...

Revision history for this message
Test (c4456517) said :
#10

So does the mouseMove() work?

Revision history for this message
obiwan-92 (obiwan-92) said :
#11

Hello,

If I remember correctly, there is a difference between the score return by the findAll function (used in the match preview) and the exists (or find) function.

See the following bug : https://bugs.launchpad.net/sikuli/+bug/1198108

To be clear, you don't see it, in the image preview, but the system find an image who close enough to the one you're looking for.
To prevent this, try to increase the low similarity score.

Regards.

Revision history for this message
obiwan-92 (obiwan-92) said :
#12

Hum...

Wait a minute, I missed the comment #2.
Well, that's odd. Try to add some sleep between each elif, just in case.

Revision history for this message
Best RaiMan (raimund-hocke) said :
#13

A construction like this only makes sense, if all images mentioned in the exists() might be visible on the screen with the intention, that the first match ends the if ... elif ... elif sequence.

So if the second exists() matches, the others will be skipped anyway. It is not something like a switch/case construct.
(elif in Python is only a shortcut to avoid additional indent with the next if)

So the question is: Is that your intention?

If it is, then you should use it like this (with version 1.0.1):
if exists("Wl.png", 0):
          hover()
          click("1390901431169-1.png")
 elif exists("1391427379709.png", 0):
          hover()
          click("1390901695089.png", 0)
 elif exists("1391402658828.png"):
          hover()
          click("4.png")
 elif exists("1390903477117.png"0):
          hover()
          click("1390903521525.png")
 elif exists("1391424371172.png", 0):
          hover()
          click("1391424719725.png")

-- exists(..., 0)
this returns after the first trial (found or not), in your case every exists lasts 3 seconds if not found, so the whole sequence if nothing is found lasts 15+ seconds.
So with your version the final click in the possible 5th elif would happen after about 20 seconds.
with the above optimised version possibly after less than 5 seconds.

-- hover()
is a shortcut for hover(getLastMatch) which avoids an additional search for the same image, that just was found.

Revision history for this message
Vaishali (vaishoo-vasu) said :
#14

Thanks RaiMan, that solved my question.