exists() and click(): How to know that click happened?

Asked by Chaoyang Shi

When I use exists(), I set the parameter to the path of the image file, but it doesn't work, I used click() to verify that the path to the image file is correct, what do I do to make exists() find the image from the storage file and recognize it on the screen

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:

This question was reopened

Revision history for this message
Chaoyang Shi (elementary-student) said :
#1

I used click() to verify the image path, click() got the job done and the result was correct

Revision history for this message
Chaoyang Shi (elementary-student) said :
#2
Revision history for this message
RaiMan (raimund-hocke) said :
#3

System, Java and SX version ?

please post an example exists(...).

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

System, Java and SX version ?

please post an example exists(...).

Revision history for this message
Chaoyang Shi (elementary-student) said :
#5

My version is 2.0.5.jar
Strangely, just when I ran it again, exists() ran successfully, however, in the afternoon, it bothered me for two hours

The problem is the final if statement。In the afternoon, click() did not run when the if statement was present
-------Below is my code--------

import shutil
import os

#Gets the absolute path to the current file
user_file = os.path.realpath("sikulix")

#Gets the path to the parent directory where the current file is located
user_file = os.path.dirname(os.path.dirname(user_file))

#Read the WORLD_FLIPPER-Auto_Land.sikuli file in the sikulix directory
user_file = os.path.join(user_file, "sikulix","test1.sikuli")

#The user sets the region to determine the play area
user_img = selectRegion()
x = 95
y = user_img.getY()
if x > user_img.getX():
    x = user_img.getX()
game_Area = user_img.offset(-x, -y)
Screen = game_Area.setW(500).setH(1080)
sleep(3)

def Capture():

    #Screenshot of login reward obtained
    game_Area = Screen

    #Reduce the area
    Login_rewards_L = game_Area.setW(game_Area.getW()/2).setH(game_Area.getH()/9)

    #Change the region
    Login_rewards_L = Login_rewards_L.offset(130, 220)

    #Save the picture to start
    Login_rewards_img = Login_rewards_L
    img_name = "Login_rewards"
    Login_rewards_img_S = Login_rewards_img

    #Create a picture storage path
    Lr_img_L = os.path.join(user_file, img_name+".png")

    #Move the image to the set path and change the name to img_name
    Login_rewards_img = capture(Login_rewards_img)
    shutil.move(Login_rewards_img, Lr_img_L)

    #Screenshot of the announcement "Close" button
    #Move down vertically from the "Login" screenshot
    Shut_down = Login_rewards_L.offset(-80, 480)
    Shut_down = Shut_down.setH(80)
    Shut_down_X = Shut_down.getX()
    img_name = "Shut_down"
    Sd_img_L = os.path.join(user_file, img_name+".png")
    Shut_down_img = capture(Shut_down)
    shutil.move(Shut_down_img, Sd_img_L)

    if exists(Sd_img_L):
        SD = find(Sd_img_L)
        click(SD)

Capture()

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

 if exists(Sd_img_L):
        SD = find(Sd_img_L)
        click(SD)

With this code, nothing happens at all, if the image does not appear within 3 seconds (standard wait time), because exists returns None in this case and the if branch is skipped.

If you want to know about Not-Found, you have to add an else branch.

A shorter snippet is this with the same effect as your if construct:
click(exists(Sd_img_L))

If the image appears within 3 seconds, it is clicked, else nothing happens at all.

with click(exists(Sd_img_L, 0)) there will be only one search - hence no wait for the image.

If you only wanted to know wether the click happens or not:
if not click(exists(Sd_img_L, 0)): print "not clicked"

Revision history for this message
Chaoyang Shi (elementary-student) said :
#7

Thanks RaiMan, that solved my question.