Script Stopping

Asked by Smizzy

I have a script similar to this

if exists(image.png)
    click(image.png)

The issue is that the image im searching for sometimes disappears inbetween those two lines of code. The script cannot click on it fast enough and stops running.

How can I either make the mouse react faster or make it not quit the script if its unable to click on the image?

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Mark Weisler (mark-weisler-saratoga-ca) said :
#1

On Aug 2, 2014, at 5:32 PM, Smizzy wrote:

> New question #252546 on Sikuli:
> https://answers.launchpad.net/sikuli/+question/252546
>
> I have a script similar to this
>
> if exists(image.png)
> click(image.png)
>
> The issue is that the image im searching for sometimes disappears inbetween those two lines of code. The script cannot click on it fast enough and stops running.
>
> How can I either make the mouse react faster or make it not quit the script if its unable to click on the image?
>
> --
> You received this question notification because you are an answer
> contact for Sikuli.
>

Please try it with this structure...

# --- using exception handling
# every not found in the try block will switch to the except block
try:
        find("path-to-image")
        pass # it is there
except FindFailed:
        pass # we miss it

--
Mark Weisler
PGP Key ID 68E462B6
PGP Key fingerprint 87D5 A77B FC47 3CC3 DFF0 586D 23FF F8B4 68E4 62B6

Revision history for this message
Smizzy (mad-dugas) said :
#2

Thanks for the fast reply!

How would i incorporate the click(image.png) into that structure?
And would this replace the exists(image.png) ?

Revision history for this message
Eugene S (shragovich) said :
#3

Hi,

Just 2 comments about the proposed solution:

1. If you are using Sikuli 1.0.1, you might not be able to use "FindFailed" exception straight away (see bug#1216338). As a workaround, add the following line to your code:

import org.sikuli.script.FindFailed as FindFailed

2. To make your script run faster, use the approach offered by RaiMan to avoid unnecessary waiting for not existing imagesby using "0" parameter for "wait" and "exist" functions.

As a result your code might look something like this:

import org.sikuli.script.FindFailed as FindFailed

try:
        wait(image, 0)
        print ("Image %s found ) %(image)
except FindFailed:
        print ("Image %s NOT found)

Eugene

Revision history for this message
Jonas Maurer (jonas-maurer) said :
#4

use

match = exists(image.png)
if match:
    click(match)

this code will search for the image, and if found, click on it's location immediately without searching twice.

(do not use exceptions to solve this. It is very bad practice.)

Can you help with this problem?

Provide an answer of your own, or ask Smizzy for more information if necessary.

To post a message you must log in.