Appium is sometimes clicking on wrong element

I am trying to click on an element on my Android Hybrid app.
Sometimes it clicks on correct element and sometimes it’s clicking on incorrect element.
I am stuck.Can anyone please help

You need to provide more details, like the code you are using, screen shot, your application code etc…with generic statment its difficult to guess where the issue is?

@kumarsiva-

Please check

My Code-

WebElement elem=driver.findElement(By.xpath(“//android.view.View[@resource-id=‘cat-1’]”));
elem.click();

Response from Appium-
info: → POST /wd/hub/session/6a503d8f-a037-4137-aa9c-00cc3fbda013/element {“using”:“xpath”,“value”:“//android.view.View[@resource-id=‘cat-1’]”}

info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: [“find”,{“strategy”:“xpath”,“selector”:“//android.view.View[@resource-id=‘cat-1’]”,“context”:“”,“multiple”:false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“xpath”,“selector”:“//android.view.View[@resource-id=‘cat-1’]”,“context”:“”,“multiple”:false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding //android.view.View[@resource-id=‘cat-1’] using XPATH with the contextId: multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[CLASS=android.view.View, INSTANCE=53]
info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,“value”:{“ELEMENT”:“7”}}
info: [debug] Responding to client with success: {“status”:0,“value”:{“ELEMENT”:“7”},“sessionId”:“6a503d8f-a037-4137-aa9c-00cc3fbda013”}
info: ← POST /wd/hub/session/6a503d8f-a037-4137-aa9c-00cc3fbda013/element 200 345.093 ms - 87 {“status”:0,“value”:{“ELEMENT”:“7”},“sessionId”:“6a503d8f-a037-4137-aa9c-00cc3fbda013”}
info: → POST /wd/hub/session/6a503d8f-a037-4137-aa9c-00cc3fbda013/element/7/click {“id”:“7”}
info: [debug] Pushing command to appium work queue: [“element:click”,{“elementId”:“7”}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“element:click”,“params”:{“elementId”:“7”}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: click
info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,“value”:true}
info: [debug] Responding to client with success: {“status”:0,“value”:true,“sessionId”:“6a503d8f-a037-4137-aa9c-00cc3fbda013”}
info: ← POST /wd/hub/session/6a503d8f-a037-4137-aa9c-00cc3fbda013/element/7/click 200 965.850 ms - 76 {“status”:0,“value”:true,“sessionId”:“6a503d8f-a037-4137-aa9c-00cc3fbda013”}
info: → POST /wd/hub/session/6a503d8f-a037-4137-aa9c-00cc3fbda013/keys {“value”:[“”]}
info: [debug] Pushing command to appium work queue: [“setText”,{“text”:“”,“replace”:false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {“cmd”:“action”,“action”:“setText”,“params”:{“text”:“”,“replace”:false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: setText
info: [debug] [BOOTSTRAP] [debug] Using currently-focused element.
info: [debug] [BOOTSTRAP] [debug] Sending plain text to element: 
info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:0,“value”:true}
info: [debug] Responding to client with success: {“status”:0,“value”:true,“sessionId”:“6a503d8f-a037-4137-aa9c-00cc3fbda013”}
info: ← POST /wd/hub/session/6a503d8f-a037-4137-aa9c-00cc3fbda013/keys 200 3595.884 ms - 76 {“status”:0,“value”:true,“sessionId”:“6a503d8f-a037-4137-aa9c-00cc3fbda013”}

Try to request that your development team insert certain tags to help you identify by id or text rather than xpath.

Also, make sure all the xml on your page is loaded before initiating a click command because sometimes elements will load on top of each other before positioning to their final place.

looking like ur xpath very generic may not be unique, ur application have multiple elements matching to the given xpath. try find the index of the elements and give the exact element where you want to click like

//android.view.View[@resource-id=‘cat-1’][1]

Alternatively use combination of xpaths like

//android.view.View[@resource-id=‘cat-1’] AND //*[text()=“category_text_you_are_trying_toClcik”]

Or find elements all with that xpath, and check text out of each element, if the extracted matching to the expected text then click

I dont have text attribute of this element. Is it possible to locate a element through content-desc ?

Yes you can, u can use class, resource id, content description, index etc… no limitations

Ok.I did try this xpath too-

WebElement elem=driver.findElement(By.xpath("//android.widget.Image[@content-desc=‘saree’]"));

But even with this its not clicking correct element all the times.What is happening here its clicking the element above it. I believe its calculating wrong y co-ordinate.

I am using the latest appium version that is 1.7.1.

When you are using xpath or other some element identification, it wont look for co-ordinates, probably the other element where it is wrongly clicking might also having the same Xpath, can you check the Xpath for that wrong element, in case if it is having more same, use index in your Xpath

If that is also not working then try to rewrite the code, first find the section of the element where your expected button is located, recognize that as a Webelement (Ex; A). And next with in that element try to find your expected element like A.findelement…().click

To debug this issue, you can print getPageSource output and check if element really present there.

As you’re saying it happens “sometimes” then it’s definitely timing issue, which you can catch with above method.

I checked the page source. Co-ordinate bounds of my element in page source are-

[232,540][354,652].

but

Appium server’s response is

[AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“xpath”,“selector”:"//android.widget.Image[@content-desc=‘apparels’]",“context”:"",“multiple”:false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“find”,“params”:{“strategy”:“xpath”,“selector”:"//android.widget.Image[@content-desc=‘apparels’]",“context”:"",“multiple”:false}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding ‘//android.widget.Image[@content-desc=‘apparels’]’ using ‘XPATH’ with the contextId: ‘’ multiple: false
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[CLASS=android.widget.Image, INSTANCE=5]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:{“ELEMENT”:“7”}}
[AndroidBootstrap] Received command result from bootstrap
[MJSONWP] Responding to client with driver.findElement() result: {“ELEMENT”:“7”}
[HTTP] <-- POST /wd/hub/session/867c0716-1211-442a-8f70-8911cebf04fb/element 200 435 ms - 87
[HTTP] --> POST /wd/hub/session/867c0716-1211-442a-8f70-8911cebf04fb/touch/perform {“actions”:[{“action”:“tap”,“options”:{“element”:“7”}}]}
[MJSONWP] Calling AppiumDriver.performTouch() with args: [[{“action”:“tap”,“options”:{“element”:“7”}}],“867c0716-1211-442a-8f70-8911cebf04fb”]
[AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:getLocation”,“params”:{“elementId”:“7”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:getLocation”,“params”:{“elementId”:“7”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: getLocation
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:{“x”:232,“y”:414}}
[AndroidBootstrap] Received command result from bootstrap
[AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:getSize”,“params”:{“elementId”:“7”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:getSize”,“params”:{“elementId”:“7”}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: getSize
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:{“width”:122,“height”:8}}
[AndroidBootstrap] Received command result from bootstrap
[AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“element:click”,“params”:{“elementId”:“7”,“x”:293,“y”:418}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“element:click”,“params”:{“elementId”:“7”,“x”:293,“y”:418}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: click
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:true}
[AndroidBootstrap] Received command result from bootstrap
[MJSONWP] Responding to client with driver.performTouch() result: null.

As you can see it is taking co-ordinate 293,418.

Now 418 that is y- co-ordinate is not within the bounds of my element which is-[232,540][354,652]

Which makes me confused that why is it taking the wrong y co-ordinate sometimes!

Then these 2 are not the same elements. You can cross check in debug more or ruby REPL mode

If they were two different elements then how will it click correctly sometimes?

The problem was solved by using uiautomator2.
capability.setCapability(“automationName”,“uiautomator2”);

1 Like