List, object has no attribute 'hasNext'

Asked by Maks

Hello, Colleagues.
Need your help.
I'm trying to click on all found image, previously sorted, except the last.
Here's my code:

def order(match):
    return match.y, match.x
def CLICKER():
    scr1 = Screen(1)
    pat = Pattern("1429518580337.png").similar(0.95)
    icons = scr1.findAll(pat)
    sorted_icons = sorted(icons, key=order)
    for icon in sorted_icons:
        print icon
        scr1.click(icon)
        CLICKER()
        break
CLICKER()

I'm using recursion to find image again on each step, because after one image is clicked, others change position.
Also use scr1 = Screen(1), because image on my second monitor.
It successfully clicks on all images. But I need to add condition hasNext() to do not click on the last image.
I've tried:
(1) if icon.hasNext() == 'True':
     get an [error] AttributeError ( 'org.sikuli.script.Match' object has no attribute 'hasNext' )
(2) if icons.hasNext() == 'True':
     no error - but clicks only first image
(3) if sorted_icons.hasNext() == 'True':
     [error] AttributeError ( 'list' object has no attribute 'hasNext' )

How to add hasNext() condition?
Thanks very much for you help.

Question information

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

P.S. Forget to specify - I'm adding condition before function CLICKER() incovation:
if icon.hasNext() == 'True':
     CLICKER()
Thanks.

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

Hi,

hasNext() is a Finder class method. You are trying to apply it to wrong classes.

Below is the hasNext() usage example:

finderObject = Finder("pattern.png")
imgToFindInPattern = "pattern1.png"

finderObject.find(imgToFindInPattern)

while finderObject.hasNext():
    print finderObject.next()

When the above loop is finished and you try to invoke hasNext() again, you will get False.

Eugene

Revision history for this message
Maks (fapw) said :
#3

Eugene S (shragovich), thanks for your answer.
I've tried to use Finder():

def order(match):
    return match.y, match.x
def CLICKER():
    scr1 = Screen(1)
    f = Finder ("1429541887831.png")
    pat = Pattern("1429518580337.png").similar(0.95)
    icons = scr1.findAll(pat)
    sorted_icons = sorted(icons, key=order)
    f.findAll("1429541926757.png")
    for icon in sorted_icons:
        print icon
        print f.hasNext()
        if f.hasNext() == True:
            scr1.click(icon)
            f.next()
            CLICKER(),
        break
CLICKER()
Result the same - clicks on all images including the last. It's obvios, statement (if.hasNext() == True) is always TRUE, because each iteration Finder() is the same. I'll try to use Count, but it's not so interesting.

Maybe is there a possibility to make screen of monitor each iteration and use it as Finder()?
Thanks.

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

I do not really understand your intention, since you say:
I'm using recursion to find image again on each step, because after one image is clicked, others change position.

… but with
Finder ("1429541887831.png")

you always search in the same image.

And I do not understand, why
 scr1.click(icon)

should click on Screen(1), if this does not have the coordinates (0,0) top left.
with scr1.click(icon), icon in this moment is a Match/Region with coordinates(top left) relative to the top left as (0,0) of the given image when you started the Finder("1429541887831.png").

So if you want to react on changes in your workflow and what you want to control is visible on Screen(1), then you should use these functions:

sorted_icons = sorted(list(scr1.findAll(whatever)), key=order)

this would at the beginning "freeze" the positions of the found icons.

… but still I do not really understand your intention.

Revision history for this message
Maks (fapw) said :
#5

Hello, RaiMan.
Thank for your answer.
My fault is that I have not explained properly.
Let me describe in details.
For example, we have three collapsing lists in excel (I've uploaded image and xlsx example here - https://onedrive.live.com/redir?resid=9bd675e55a67fdd0!121&authkey=!AKT5sxVlfQuWhOA&ithint=folder%2cxlsx). Each list have plus "+" near to expand it. This is image I'm looking for.

Once we clicked first "plus", it change their image - becomes "minus". And other pluses changed positions - shifted down.
That's why I'm using recursion - to find "+" again on each step. And finder("1429549493296.png") will find only first "+" (and I need only first plus, that's why I'm using "break"), but on each iteration first plus for this specific iteration will be next for default list. You can try if you have time with xlsx uploaded above and code:

def order(match):
    return match.y, match.x
def CLICKER():
    pat = Pattern("1429549493296.png").similar(0.95) # this is plus "+" image
    icons = findAll(pat)
    sorted_icons = sorted(icons, key=order)
    for icon in sorted_icons:
        print icon
        click(icon)
        CLICKER()
        break
CLICKER()

And it clicks all three pluses. That's why I want to add condition hasNext() to do not click on the last plus.
Thanks.
Tell me please if you need any additional info.

Revision history for this message
Maks (fapw) said :
#6

Thanks Eugene S and RaiMan for help.
All is simpler than it seems.
As I iterate LIST it has no hasNext() attribute, but it has len() function. That's enough to check len() does not equal "1" and do not click() this time.
Working code:

def order(match):
    return match.y, match.x
def CLICKER():
    scr1 = Screen(1) #or Screen(0) for default monitor
    pat = Pattern("1429549493296.png").similar(0.95) # this is plus "+" image
    sorted_icons = sorted(list(scr1.findAll(pat)), key=order)
    for icon in sorted_icons:
        if len(sorted_icons) != 1:
            click(icon)
            CLICKER()
        break
CLICKER()

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

well done ;-)

thanks for feedback.