How to click inside of a findAll?

Asked by Comprox

I want to find all of a specific image, and then click a specific spot within each image. I can find a tutorial for each step (findAll) and x.inside but when I try and merge them, it just doesn't work. I tried this code:

for x in findAll(image):
   click(x.inside().find(image))

but it throws an error:

capture: java.awt.Rectangle[x=0,y=0,width=1280,height=1024]
2 matches found
0 matches found
[sikuli] click 1 times
[sikuli] [Error] source lineNo: 2
[sikuli] [Error] Traceback (innermost last):
  File "C:\DOCUME~1\GANDER~1\LOCALS~1\Temp\sikuli-tmp47365.py", line 2, in ?
  File "C:\Program Files\Sikuli\sikuli-ide-full.jar\Lib/python/edu/mit/csail/uid/Sikuli.py", line 438, in click
 at edu.mit.csail.uid.SikuliScript._getCenterX(SikuliScript.java:751)

 at edu.mit.csail.uid.SikuliScript._click(SikuliScript.java:788)

 at edu.mit.csail.uid.SikuliScript._click(SikuliScript.java:772)

 at edu.mit.csail.uid.SikuliScript.click(SikuliScript.java:234)

 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

 at java.lang.reflect.Method.invoke(Unknown Source)

java.lang.NullPointerException: java.lang.NullPointerException

Thanks

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
RaiMan (raimund-hocke) said :
#1

the x in your case is a match object, for witch inside() is not defined.

you have to convert the x into a subregion object first, because only this knows inside()
click(Subregion(x.getX(), x.getY(), x.getW(), x.getH()).inside().find(image))

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

Sorry, you should still get an error, because I just learned, that Subregion returns Matches (not only one match object).

this means, you may have to write;
click(Subregion(x.getX(), x.getY(), x.getW(), x.getH()).inside().find(image).region) # returns the first match

Revision history for this message
Sergey Darovskih (darovskih) said :
#3

This script works for me. But if the second image is not inside the first, I get the same error as you.

just check if you found something

for x in findAll( ):
   if (x.inside().find( )):
      click(x.inside().find( ))

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

I looked at the class definitions once more: findAll returns Matches and Match.inside() is defined too (as for Subregion).

That means, this should work and should not bring an error.

for x in findAll(img1)
    y = x.inside().find(img2)
    if (y)
        click(y)

What I logically don't understand: if you find any image1, than image2 must be inside, since its already contained within image1.

This means, if x.inside() for a Match is valid, this should work:

for x in findAll(img1)
    click(x.inside().find(img2)) # if any img1 are found, img2 must be there

if it brings an error, this means, that there is some problem with the implementation of the Match.inside().

and so I come back to my proposed circumvention: make a Subregion from the match.

for x in findAll(img1)
    click(Subregion(x.getX(), x.getY(), x.getW(), x.getH()).inside().find(image).region)

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

Sorry, left the colon!!!

for x in findAll(img1):
    click(Subregion(x.getX(), x.getY(), x.getW(), x.getH()).inside().find(image).region)

Revision history for this message
srijith (electronicmails1) said :
#6

hi RaiMan
         I used your latest example but I still get a error like "AttributeError: 'javainstance' object has no attribute 'region' "

 Please provide more information

Can you help with this problem?

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

To post a message you must log in.