code works once

Asked by jeff

how can a code that worked on a previous line work and then a copy and past of that line not work

How will part of the code work on the first go run into the error above then not work at all the next go

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
jeff
Solved:
Last query:
Last reply:
Revision history for this message
Harry Readinger (temporary22) said :
#1

It would be helpful if we could see the code in question. Otherwise it is almost impossible to tell what is happening.

Revision history for this message
jeff (smith15022) said :
#2

click("1320241172682.png")
wait(2)
click("1320241172682.png")
wait(2)
click("1320241172682.png")
wait(2)
click("1320241172682.png")
wait(2)
click("1320241172682.png")
wait(2)
click("1320241172682.png")
wait(2)
click("1320241172682.png")
wait(2)
click("1320241172682.png")
wait(2)
click("1320241172682.png")
wait(2)
click("1320241172682.png")
wait(2)
click("1320241215290.png")
wait(2)
click("1320241215290.png")
wait(2)
click("1320241215290.png")
wait(2)
click("1320241215290.png")
wait(2)
click("1320241215290.png")
wait(2)
click("1320241215290.png")
wait(2)
click("1320241215290.png")
wait(2)
click("1320241215290.png")
wait(2)
click("1320241215290.png")
wait(2)
click("1320241215290.png")
wait(2)
click("1320241396712.png")
wait(1)
click("1320241528063.png")
wait(2)
click("1320241396712.png")
wait(1)
click("1320241528063.png")
wait(2)
click("1320241396712.png")
wait(1)
click("1320241528063.png")
wait(2)
click("1320241396712.png")
wait(1)
click("1320241528063.png")
wait(2)
click("1320241396712.png")
wait(1)
click("1320241528063.png")
wait(2)
click("1320241396712.png")
wait(1)
click("1320241528063.png")
wait(2)
click("1320241396712.png")
wait(1)
click("1320241528063.png")
wait(2)
click("1320241396712.png")
wait(1)
click("1320241528063.png")
wait(2)
click("1320241396712.png")
wait(1)
click("1320241528063.png")
wait(2)
click("1320241396712.png")
wait(1)
click("1320241528063.png")
wait(2)

now looking a bit more i think it maybe because the images are not found on screen what would the code be for it to check for the image then click if there or if not move on

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

have a look at exists()

e.g.

if not exists(image): print "Error"; exit(1)
click(getLastMatch)

or use the interactive options with FindFailed situations (see docs)

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

This is your script more complex:

for i in range(10): # repeat 10 times
    click("1320241172682.png")
    wait(2)
for i in range(10): # repeat 10 times
    click("1320241215290.png")
    wait(2)
for i in range(10): # repeat 10 times
    click("1320241396712.png")
    wait(1)
    click("1320241528063.png")
    wait(2)

What is then sense of it? If you click something, then something should happen at least after a while. So why click over and over again?

the normal workflow is to click something and then wait for the expected next screen situation (e.g. with exists()) and then proceed with the next action.

Revision history for this message
jeff (smith15022) said :
#5

so i would use

exists(img)
   if exists click

this does not work just hope it will help someone understand what im trying to do. i looked at the exists doc and brought me no further

Revision history for this message
jeff (smith15022) said :
#6

its would be clicking different things but they have the saem img after the click there image changes

Revision history for this message
jeff (smith15022) said :
#7

Let me break this down a bit more i am looking for the correct coding to

If the image is there click on it up to 10 times. if it does not exist move on to the next image, search for that and if found click on that image up to 10 times

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

ok, then lets base on this version, because it is more compact and does the same:

# block 1
for i in range(10): # repeat 10 times
    click("1320241172682.png")
    wait(2)

# block 2
for i in range(10): # repeat 10 times
    click("1320241215290.png")
    wait(2)

# block 3
for i in range(10): # repeat 10 times
    click("1320241396712.png")
    wait(1)
    click("1320241528063.png")
    wait(2)

If I understand you write, in block1 you click ten times on the same image, that changes, when it is clicked, so in the next turn another of these equal images is clicked.
This should work, when there are always 10 images and if 2 seconds are enough to wait for the clicked image to change.

So lets assume there are max 10 images (might be less):

# block 1
for i in range(10): # repeat 10 times
    if not exists("1320241172682.png"): break
    click(getLastMatch())
    wait(2)

This version will end the block1 loop if no more images are found and proceed with block2.

A general alternative is:

matches = findAll("1320241172682.png")
for m in matches:
       click(m)

here you would not have to wait for the images to change, because all images where found with findAll(), before we start to click.

hope it brings you further down true road to Sikuli-Happiness.

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

ok, this is magic ;-)

while you thought about your comment #7 I could read your brain and answer in parallel ;-)

really magic LOL

Revision history for this message
jeff (smith15022) said :
#10

lol thank you Rai and watch out u dont read my mind to much im told there some sick shit in there! that has help me do exactly what i needed to do now can some one tell me how to get the script to repeat endless untill hot key is pressed. thanks again for the help and not flameing the n00b

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

be sure, noobs are one of my hobbies ;-)

--- repeat endless
(for some background read faq 1437)

your stuff:

select all lines in your script, that shall be repeated, and press tab. this should insert an indent in front of these lines. this is our block to be repeated. before the first line insert (without indent):

while True:

--- hotkey to terminate
easiest way: Sikuli's standard abort key: shift-alt-c

crash option: hide the app window, so that the images are no longer found - script will crash with a FindFailed error.

professional option: implement a private hotkey (see docs, not really for noobs ;-). makes only sense, if you want the script to process further after being interrupted.

Revision history for this message
jeff (smith15022) said :
#12

found my own answer

while True :
    if not exists("th.png"): break

Ty again for the help!

Revision history for this message
jeff (smith15022) said :
#13

lol i was in your head this time!