How to Click Invisible Android UI Elements with UiAutomator by Calculating Coordinates
When UiAutomator reports controls with bounds [0,0][0,0] and they cannot be clicked, you can compute click positions from a previously‑found element’s size and use those coordinates to tap the left or right half of the target control.
While learning UiAutomator, the author encountered UI elements whose bounds were reported as [0,0][0,0], making them impossible to click directly, especially in complex hierarchies such as the JD app coupon page.
To work around this, the author proposes using the size of a previously‑found control to calculate a reliable click coordinate and then performing the click on either the left or right half of the target element.
public void getUiObjectByResoureIdAndclickRightHalf(String id) throws UiObjectNotFoundException {
// Get control size
Rect sss = getUiObjextByResourceId(id).getBounds();
// Compute offset to the right half (center + width/4)
clickPiont(sss.centerX() + sss.width() / 4, sss.centerY());
}
public void getUiObjectByResoureIdAndclickLeftHalf(String id) throws UiObjectNotFoundException {
// Get control size
Rect sss = getUiObjextByResourceId(id).getBounds();
// Compute offset to the left half (center - width/4)
clickPiont(sss.centerX() - sss.width() / 4, sss.centerY());
}The two helper methods first retrieve the Rect of the UI object via getUiObjextByResourceId(id).getBounds(), then calculate a point offset from the rectangle’s center by a quarter of its width, and finally invoke clickPiont(x, y) to simulate the tap.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
