How to get the acutal co-ordinates of an element on Android Real device

Hi,

I’m automating on a Android Real Device, For an element, the getLocation() API returns the x &y co-ordinates of an element, but when i try to perform actions like tap, swipe, with the co-ordinates which is been fetched from getLocation() API the action does not seem to be performed on expected element.

The co-ordinates fetched through getLocation() API differs from the co-ordinates shown by the Android Devices Pointer Location.

How should tap,swipe be performed based on the elements co-ordinates using TouchActions API.

Kindly provide guidance in the above scenario.

x,y is left upper corner of some element. i suggest first to use element width and height to get element center and tap it instead.

e.g. x+w/2 , y+h/2

Hi
Thanks for your reply.
I did try getting the element size, based on the height and width get the centre position of the element and perform a tap but observed that on android tab screen resolution is different.
should I set any capabilities for screen resolution based on the device type.

Thanks.

“should I set any capabilities for screen resolution based on the device type.” - no.

Thanks Aleksei…

Observed that the x and y co-ordinates Appium is fetching is exactly half of the device x and y co-ordinates on android real device.

Please let know the reason…

Try use Values of X and Y values returned by getLocation()
for example
int leftX = element.getLocation().getX();
int rightX = leftX + element.getSize().getWidth();
int middleX = (rightX + leftX) / 2;
int upperY = element.getLocation().getY();
int lowerY = upperY + element.getSize().getHeight();
int middleY = (upperY + lowerY) / 2;

6 Likes

Is there a method for c#?

I believe it’s the same for c#.

C# is not recognizing the getlocation() command.Says that android element does not contain it

have you got your answer, I am in same state.

will you provide your solution, that may help me .

Thank u, worked fine for me.

Try use getCenter(), example:

int X = element.getCenter().x;
int Y = element.getCenter().y;

good luck!

thanks its worked :slight_smile:

in C#, it is called .Location()

int startX = startElement.Location.X;
int startY = startElement.Location.Y;

This worked as a charm

I was having issue with ios and worked for ios too.