public void middleCordinate(WebElement element){
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;
}
This is the method that I use to get the center coordinates of a button to click in run time. But the resultant middleX and middleY is not same as the center coordinates that I get using appium server “Tap By Coordinates” option. Using the above method I get (x = 540, y = 2100) , but the correct coordinates that I get from “Tap By Coordinates” option via Appium server is (x = 551, y = 1603). I need to know why this difference?. And how to get same coordinates as appium server “Tap By Coordinates” option.
I have updated the calculation as you’ve mentioned but then too the coordinates that I get are not matching with the coordinates that I get with “Tap By Coordinates” using appium server
The new coordinates that I get after updated calculation is (x = 579, y=2380) which is not what I wanted when I manually checked the center of the button it is (x = 551, y = 1603). If there is any other way to exactly get the center coordinates that match with the “Tap By Coordinates” please let me know, thanks for your replies
Ok, can you give us the values for leftX, rightX, upperY, and lowerY? It’s not like there is a whole lot that can go wrong here, but without the raw values (or the app itself) it’s hard to see where this is going wrong.
Also, please stick to the variable names you’ve assigned. When you say middleX in your code for example and then refer to it as simply ‘x’ it is confusing.
Oh, never mind. I can see that @mykola-mokhnach got it right above. I didn’t see that you were adding the leftX to the middleX twice and upperY to lowerY twice. Here is the code:
int leftX = element.getLocation().getX();
int rightX = element.getSize().getWidth();
int middleX = rightX + (leftX / 2);
int upperY = element.getLocation().getY();
int lowerY = element.getSize().getHeight();
int middleY = upperY + (lowerY / 2);
I would rename the ‘rightX’ and ‘lowerY’ to better variable names, like ‘height’, and ‘width’.