How to click on a text with same description

Asked by Sandeep

Hi,

I am new to sikuli.

I have 2 text's with same description and i am not able to differentiate between them. I want to differentiate between them and click on each of them. As of now, sikuli is randomly clicking on these texts

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
Revision history for this message
Sandeep (sandy-mechie) said :
#2

Hi Raiman,

Thanks for your response. But i think i need to click based upon the test case and not one by one as per my previous post. My apologies . So Can i do a click based on coordinates.

I am first finding the coordinates by using

X = Env.getMouseLocation()

So X is something like (100,200)

Using these coordinates can i do a mouse click?

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

Nevertheless you can use findAll() and it helps you better to decide which one to click:

matches = list(findAll(Pattern(img).similar(0.95))) # similarity as high as possible to avoid false positives
matches.sort(lambda m1, m2: m1.y > m2.y)

now matches contains the found matches in top down order, supposing the targets are below each other.

Since you know the number of needed matches, you should check:
if len(matches) != 2:
    print "findAll() did not work as expected"
    exit()

now you know:
matches[0] # is the upper one
matches[1] # is the lower one

and you can do with either one what you want, e.g.
click(matches[0]) # click the upper one

Revision history for this message
Sandeep (sandy-mechie) said :
#4

Hi Raiman,

I am getting the following error when i use the above code

matches = list(findAll(Pattern("my.png")).similar(0.95))
AttributeError: 'org.sikuli.script.Finder' object has no attribute 'similar'

Am i doing something wrong?

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

matches = list(findAll(Pattern("my.png").similar(0.95)))

mind the braces.

Revision history for this message
Sandeep (sandy-mechie) said :
#6

yup..thanks a ton for your time and help

Revision history for this message
Sandeep (sandy-mechie) said :
#7

Thanks RaiMan, that solved my question.

Revision history for this message
Sandeep (sandy-mechie) said :
#8

just one question

i am using the findall method, its returning the correct number of objects
For Ex: 3

the objects are located side by side

so when i run this code

Click(sorted_icons[0])
Click(sorted_icons[1])
Click(sorted_icons[2])

everytime it makes a random click on the objects...there is no particular order....

Like for 1st time the order of click might be 11st object, 3rd object and then 2nd obj

if i re-run the code the order might change to 3rd object, 1st object and then 2nd obj

Is this a bug that its not able to store the index or am i going wrong somewhere?

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

when the icons are side by side, you have to use the x coordinate for sorting:

matches.sort(lambda m1, m2: m1.x > m2.x)

Revision history for this message
Sandeep (sandy-mechie) said :
#10

ok got it ...thanks again