Region.find or Region.findAll problem

Asked by Jose L

Hi All,

I have attempted to use a loop with Region.find() and Region.findAll() to perform actions on a web page that contains the same icon multiple times. I first set the region and then go through the process (see code below).

My problem is that the region I have set is not respected by the find actions as icons outside of that range (region) are being clicked in the process with results in exception errors being thrown.

Any ideas of what I may be doing wrong or is this a bug?

Thanks in advance for your input!

reg = Region(413,248,680,510)
while reg.inside().find("1376053585710.png"):
    click("1376053585710.png")
    find("1376053640208.png")
    click("1376053656049.png")
    find("1376053684932.png")
    click("1376053695853.png")

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:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

You are not using the defined region to restrict the searches:

click() works on the whole screen

but
reg.click()
would only search in the Region reg

this should principally work:

reg = Region(413,248,680,510)
while reg.exists("1376053585710.png"):
    click(reg.getLastMatch())
    find("1376053640208.png")
    click("1376053656049.png")
    find("1376053684932.png")
    click("1376053695853.png")
else:
    print("no more found)

comments:
-- exists
in while and if constructs use exists instead of find, since it avoids the unnecessary exception if not found

-- getLastMatch
The last succesful find operation is remembered by a region and can be used to act on it with click(reg.getLastMatch()), which on top avoids an additional search (which exactly is your problem in this case).

--- BTW: if 1376053585710.png ...
... is your same icon, that might be there multiple times in the Region reg, than you loop will only work, if the processed icon vanishes or changes in a way, that it is no longer found, after being processed. Otherwise you might always get the same icon with
 reg.exists("1376053585710.png").

The only chance to solve this is findAll() (see docs)

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

I just realized, that you are talking about 1.0.1

This is even easier:

reg = Region(413,248,680,510)
while reg.exists("1376053585710.png"):
    reg.click()
    find("1376053640208.png")
    click("1376053656049.png")
    find("1376053684932.png")
    click("1376053695853.png")
else:
    print("no more found)

... since beginning with 1.0.0 reg.click() will click the last successful match in reg, if one exists or the center otherwise.

Revision history for this message
Jose L (jrlopezp) said :
#3

RaiMan,

Thank you! This solved my problem. I went by the documentation examples but failed to notice the region (reg) annotation or totally missed the concept.

I appreciate your response.

Regards,

Jose L

Revision history for this message
Jose L (jrlopezp) said :
#4

Thanks RaiMan, that solved my question.