Appium Android How to tap using the TEXT value

11,840

Solution 1

Found the solution. I had to write the following code in my test:

capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);

Now I can use the TEXT value by writing that song name in the XPath like this:

By.xpath("//*[@text='" + element + "']");

Here element is the text value shown in UIAutomatiorViewer.

Solution 2

You have got the class name wrong in the xpath.

It should be the following based on what I see on uiautomatorviewer

By.xpath("//android.widget.TextView[@text='"+songname+"']");

Solution 3

There is no need to using custom xpath for this.
First you have to take all the elements with that resource ID and click on required element if it contains the text on which you want to click.
You can use the below code.

    List<WebElement> elements = driver.findElements(By.id("resource-id"));
    For(WebElement element:elements)
    {
        if(element.getText().equals("textProperty"))
        {
            element.click();
            break;
        }
    }
Share:
11,840
Varun Bali
Author by

Varun Bali

Updated on June 04, 2022

Comments

  • Varun Bali
    Varun Bali almost 2 years

    I want to play a song by making Appium tap the song name. Is there a code which can allow me to tap any element by using the TEXT field? I cannot use the resource id field here as there will be multiple songs later on. I tried using By.xpath("//android.widget.EditText[@text='"+song name+"']"); but that did not work.

    enter image description here

  • Varun Bali
    Varun Bali over 6 years
    Yeah that was a typo but unfortunately the solution you wrote is the same I have tried in the code & it did not work.
  • Mike Collins
    Mike Collins over 6 years
    This is the correct way to find it. What is returned when you attempt to to run with this? It's possible there could be a view layering issue that doesn't allow you to tap where the element is on the z-axis. I've seen this before and it's usually fixed by having my developers clean up a broken view model. What happens when you try tapping the area in which that element is (you can make up coordinates inside the bounds)?
  • Varun Bali
    Varun Bali over 6 years
    Your first solution did not work. Unfortunately I cant use resource id here because there are going to be multiple songs later on & every one of them will have the same resource id. I am trying to write a framework & so I have to make it as much generic & simple for a non programmer as possible. All he/she needs to do is mention the name of the song according to the text value as shown in the UIAutomator. I dont understand why this is not working in Appium.
  • barbudito
    barbudito over 6 years
    1st. The second solution will work even faster even with every song having same ID... As you can see is a @resource-id='' AND @text=''. 2nd. Can you try changing //android.widget.TextView for //* and if you have no luck... Maybe this (small piece of text)? By.xpath("//*[contains(@text, 'Ishq')]");
  • barbudito
    barbudito over 6 years
    Btw... Your screenshot is from AndroidAutomationViewer.bat. Why don't you try to inspect from appium itself? What is your Appium version? I suggest you to update to the last version of appium-desktop
  • Varun Bali
    Varun Bali over 6 years
    Still not working. Appium keeps on searching at the FindElement method for infinite duration even though I am using only explicit wait. with both of your suggestions. I also tried searching via UIAutomator technique by using MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"com.xefyr.ridez:id/tv_song_title\"‌​).textContains(\"Ish‌​q\");");. In the debug window I can see [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[CONTAINS_TEXT=Ishq, RESOURCE_ID=com.xefyr.ridez:id/tv_song_title] but still it fails with no element found message.
  • barbudito
    barbudito over 6 years
    And what about using appium-desktop?
  • Varun Bali
    Varun Bali over 6 years
    I do not see any feature in Appium Desktop 1.3.1 which can allow me to inspect. I am on Windows 10. Am I missing something?
  • barbudito
    barbudito over 6 years
    Appium Desktop 1.3.1 is an old version... Can you update to newer versions? Maybe > 1.7
  • Varun Bali
    Varun Bali over 6 years
    1.3.1 is the version of the GUI. Otherwise Appium version that is shipped in is 1.7.2