How do I get Sikuli to click randomly?

Asked by Jeff McGuire

Hey, I was wondering if there is a way to get sikuli to click randomly within a region, or drag-and-drop. I saw the python Random module, but I don't know how to use it.
Thanks

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

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

import random
r = someRegion
for i in range(100): # click randomly 100 times
    locx = int(r.w * random.random())
    locy = int(r.h * random.random())
    click(Location(locx, locy))
    wait(0.3) # optional: some time for the GUI to react

random.random() returns a float between 0 and 1

Revision history for this message
Jeff McGuire (couchdragonmedia) said :
#2

Thanks RaiMan, that solved my question.

Revision history for this message
Jeff McGuire (couchdragonmedia) said :
#3

Is there a way so that sikuli will only click on a certain color or pattern while also clicking randomly in a region? If you cant help me thats fine, but i was still wondering.

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

Of course, I can answer your question ;-)

--- so that sikuli will only click on a certain color

In the current version you have to step down to the Java Robot, which has a function getPixelColor() (the new version will have a Location.getColorAt(x.y) ).

the basics:
import java.awt.Robot as JR
import java.awt.Color as JC
print JR().getPixelColor(getCenter().x, getCenter().y) == JC.RED
print JR().getPixelColor(getCenter().x, getCenter().y) == JC.WHITE
print JR().getPixelColor(getCenter().x, getCenter().y) == JC(255,255,255)

To get more info, you have to look into the respective JavaDocs of Robot and Color.

with the snippet from comment #1 (assuming to only want pure red points):

import java.awt.Robot as JR
import java.awt.Color as JC
import random
rob = JC()
col = JC.RED
r = someRegion
for i in range(100): # click randomly 100 times
    locx = int(r.w * random.random())
    locy = int(r.h * random.random())
    while not rob.getPixelColor(locx, locy) == col:
        locx = int(r.w * random.random())
        locy = int(r.h * random.random())
    click(Location(locx, locy))
    wait(0.3) # optional: some time for the GUI to react

BeAware: if less then 100 red points will ever be found, this will loop endless.

--- so that sikuli will only click on a certain pattern
this only makes sense if you want to say:
click only if in a certain region around the currently selected point the pattern (= image) is present

If this is what you want, here you are:

import random
r = someRegion
w = 50 # width and
h = 50 # heigth around point to check for pattern
p = "some-image.png"

for i in range(100): # click randomly 100 times
    locx = int(r.w * random.random())
    locy = int(r.h * random.random())
    while not Region(locx-w/2, locy-w/2, w, h).exists(p, 0):
        locx = int(r.w * random.random())
        locy = int(r.h * random.random())
    click(Location(locx, locy))
    wait(0.3) # optional: some time for the GUI to react

Same here with looping endless: if the pattern is never found.

Can you help with this problem?

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

To post a message you must log in.