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


#1

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.


#2

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


#3

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.


#4

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


#5

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…


#6

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;


#7

Is there a method for c#?


#8

I believe it’s the same for c#.


#9

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


#10

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

will you provide your solution, that may help me .


#11

Thank u, worked fine for me.


#12

Try use getCenter(), example:

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

good luck!