← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #670847]: loop click one image only from a list

 

Question #670847 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/670847

    Status: Open => Answered

Alex proposed the following answer:
Sounds like you want to do a for loop. Python lists are iterate-able.
Example:

image_list = ['image1.png', 'image2.png', 'image3.png']

for image in image_list:
    click image

This iterate through the list and click every image, which doesn't seem
like what you want to do. It sounds like you want to click a different
image every time you loop through. Something like this might work:

image_list = ['image1.png', 'image2.png', 'image3.png']
i = 0
while true:
do_stuff()
click(image_list[i%len(image_list)])
i += 1
do_more_stuff()

Here's the basics of what I did there:

1. Initialize a variable to track what the loop count is. 
2. Do your stuff before the loop. 
3. Use the modulus operation to determine what index of the list to call. It works like this in my example:
len(image_list) = 3   # or whatever the length of your list i

i = 0
0 % 3 = 0
select the list element at index 0.

i = 1
1 % 3 = 1
select the list element at index 1.

i = 2
2 % 3 = 2
select the list element at index 2.

i = 3 # now we wrap back around to index 1
3 % 3 = 0
select the list element at index 0. 

This allows you to run an infinite loop that will sequential select one
image from your image list each time.

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.