Click button beside a label using Java

Asked by akbar

I want to click a button which is located beside a label on a webpage using the Sikuli Java lib. But it fails at the click step saying it couldnt find the button image. Not sure if Match is the correct class to be used in here. I would appreciate if someone can help on this?

code:-
Match m = sample.find("lblFileUpload.png").right().find("btnUpload.png");
sample.click(m);

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

principally ok.

the problem might be caused by right()

this is the region to the right of sample.find("lblFileUpload.png"), but with the height of this match/image.
So if the button does not totally lies inside, the find will fail.

To verify:
Match m = sample.find("lblFileUpload.png")
m.highlight(3);
m = m.right().exists("btnUpload.png");
if (m != null) sample.click(m);
else: System.exit();

Revision history for this message
akbar (mohammed-akbar-ali) said :
#2

Thanks for the reply.
the label is getting highlighted but it still does not click on the button. m is null after the exists() step. The scenario i have is to click on a button "Upload" and label is "Click to Upload". In Chrome button click is working fine but failing in Firefox as it is clicking the label instead of button. So i had to write the above logic.

Is there any other solution for this problem?r

Also as a general query,
i)do the screenshots taken on one browser work/get recognized runtime on other browsers as well?
ii)if screenshot is taken with browser maximized, will they get recognized runtime if browser window is not maximized

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

Ok, then the button with FF looks different (Chrome and FF have different rendering engines).

This is a general problem for Sikuli: even minor differences on the pixel level (might only visible for the human if you magnify the probes so you can see the pixels) will force Sikuli to fail (not found).

The only chance: have different image sets for the different situations.

In this case another possibility might be: If the button is in a "fixed" location with respect to the label in all situations, then you might use:

Match m = sample.find("lblFileUpload.png");
click(m.getCenter().offset(x,y))

You might evaluate the x,y with the IDE-Preview -> target offset tab.

--- your questions:

i) see above - depends ;-)

ii) should normally be no problem, but .... ???

Revision history for this message
akbar (mohammed-akbar-ali) said :
#4

Thanks. Offset logic worked.

Revision history for this message
akbar (mohammed-akbar-ali) said :
#5

Thanks RaiMan, that solved my question.