Define region

Asked by csy

Hi everyone,

It's pretty hard to get a simple example of Region.
I read through the notes of Region but still not manage to write a solution.

Condition:
One webpage consists of 5 similar icons, I just want to click one of those similar icons,
so I need to select a region and click the icon inside the Region.

Codes that is not working:

def Region_with():
  reg = Region(551,325,499,342)
  reg.highlight(5)
  reg.click("icon.png")

Anyone can do me a favor or just show a simple Region tutorial?
Thanks in advance!

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Roman Podolyan
Solved:
Last query:
Last reply:
Revision history for this message
Best Roman Podolyan (podolyan-roman) said :
#1

What do you get in console?

Didn't you forgot to call the function itself, did you (see the last line added by me):

=====
def Region_with():
    reg = Region(551,325,499,342)
    reg.highlight(5)
    reg.click("icon.png")

Region_with()
=====

Revision history for this message
csy (88ccsy) said :
#2

Thanks Roman Podolyan, that solved my question.

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

Ok, this might work now, but is always a bad idea to work with fixed regions like Region(551,325,499,342) (only for time of testing and trying to find the right solution).
Since this is on a webpage, the webpage must be always in the same position on screen.

And this function does not make any sense, since it has no parameters and hence always does the same.

As a general approach with Sikuli, you should search in cases like yours in regions, that you defend relative to some fixed images in your app.

I give you an example that uses the area at the top of this question page:

goal:I want to click the edit button of Status:

header = "image of text Question Information.png"
# should have width of the whole area above the details
status = "image of text Status.png"
# should be wide enough to span the edit button
edit = "image of edit button.png" #(there are more of them on the page)
mHeader = find(header)
rStatus = mHeader.below(50).find(status).below(30)
rStatus.click(edit)

or just in one statement (e.g. after having tested)
find(header).below(50).find(status).below(30).click(edit)

or if you want to reuse the header match as reference region more than once:
mHeader = find(header)
mHeader.below(50).find(status).below(30).click(edit)

and later might be
mHeader.below(50).find(language).below(30).click(edit)

or you use the target offset feature (Preview window) (preferable for fixed layouts)
click(Pattern(header).targetOffset(180, 55)) # clicks the edit button of status

the offset values depend on the center of the image and are evaluated using the target offset tab of the preview window.

These examples always work independently of the location of the webpage on the screen.

Want the example as working script: https://dl.dropboxusercontent.com/u/42895525/relative-regions.sikuli.zip

Revision history for this message
csy (88ccsy) said :
#4

Thank RaiMan for detail explanation, i will try it on!