how to get the last element of findAll

Asked by bilel

Hi,

On a page of my website, I have 5 checkbox, I need to check only the last one. The problem is that I have them on two lines like this:
************************
******************
How do I do to get the last one?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
bilel
Solved:
Last query:
Last reply:

This question was reopened

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

sort the resulting matches appropriately and select the one that you want by index.

solution for Python: http://doc.sikuli.org/region.html#Region.findAll

Revision history for this message
bilel (bilel-gasmi) said :
#2

Sorry I didn't see this page before.

Thank you,

Revision history for this message
bilel (bilel-gasmi) said :
#3

elements = reg.findAll("checkbox.png")
last_element = sorted(elements, key=lambda x:x.y)[-1]

Until yesterday this worked as expected, which means the right last element is returned, but sometimes it returns another element which obviously is not the last one in the list.

How can I make sure I'm getting the last one?

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

as far as I understood your setup, the checkboxes (cbx) are arranged this way:

(cb1) some text (cb2) some text (cb3) some text

(cb4) some text (cb5) some text

supposing, they are vertically aligned (same y value for cb4 and cb 5), than it is not predictable, that always cb5 is sortedList[-1], it might be sortedList[-2] (so it might depend on air pressure or something similarly random ;-)

so you have to sort the last two values according to their x value.

or use a lambda like this:
last_element = sorted(elements, key=lambda element: element.y * 10000 + element.x)[-1]

Revision history for this message
bilel (bilel-gasmi) said :
#5

Nice :), works like a charm now, thank you RaiMan