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.

FunTester
FunTester
FunTester
How to Click Invisible Android UI Elements with UiAutomator by Calculating Coordinates

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

UI automationmobile testingUIAutomatorClick Coordinates
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.