SikulixIDE 1.1.2 Click a random place inside an image

Asked by PowerPenguin

Hi! I'm new to Python (but not to coding) and there's something I'm trying to do but I'm not sure how to do it.

I want to click an image that appears in the screen several times (not one after other, after clicking it one time it disappears and appears again after a while), but I don't want to click it always in the center, I want to click it each time in a random place inside the image.

I've been messing with the Region class and the functions getTopLeft(), getBottonLeft() etc and the Random class of Python, I've changed my code a million times but I still can't figure it out. If you could help me it would be great. I've seen similar answers in the forum but I'm not sure how to implement them in my code.

This is what I have:

import random
m = find(imageIwantToClick)
click(m.targetOffset(random.randint(m.getTopLeft(),m.getBottomLeft()),random.randint(m.getTopRight(),m.getBottomRight())))

And I get this error:
[error] Error caused by: Traceback (most recent call last): File "C:\Users\Penguin\Documents\Sikuli Pruebas.sikuli\Sikuli Pruebas.py", line 3, in <module> l = random.randint(m.getTopLeft(),m.getBottomLeft()),random.randint(m.getTopRight(),m.getBottomRight()) File "C:\Users\Irene A\Desktop\Sikuli\sikulix.jar\Lib\random.py", line 241, in randint TypeError: unsupported operand type(s) for +: 'org.sikuli.script.Location' and 'int'

From what I understand, I always have to introduce an image inside the function click() to make it work? But then, I don't know how to click in a place that isn't an image, just a coordinate in the screen.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
masuo
Solved:
Last query:
Last reply:
Revision history for this message
Manfred Hampl (m-hampl) said :
#1

Maybe

click(location(random.randint(m.getTopLeft(),m.getBottomLeft()),random.randint(m.getTopRight(),m.getBottomRight())))

Revision history for this message
Best masuo (masuo-ohara) said :
#2

Both getTopLeft() and getBottomRight() return Location object.
random.randint() require integer.

click(Location(random.randint(m.getX(),m.getX()+m.getW()),random.randint(m.getY(),m.getY()+m.getH())))

Revision history for this message
PowerPenguin (powerpenguin) said :
#3

Thanks masuo, that solved my question.