How to find an element using partial text

Hello,

I have an application homepage and I the appium script is not able to find the element using the ID at runtime:

@FindBy(how=How.XPATH,using=("//android.widget.TextView[@text=‘FamilyProfile’]")) WebElement Family_Profile;

I have a few observations that I would like to share:

  1. The argument in the text view and the text differs
  2. When I generate the source during runtime, I have observed that for this element is being translated as “Family Profile”

I have also tried using
@FindBy(how=How.XPATH,using=("//android.widget.TextView[@text=‘Family Profile’]")) WebElement Family_Profile;

and

@FindBy(how=How.XPATH,using=("//android.widget.TextView[@text=‘Family’]")) WebElement Family_Profile;

If you would please advice on the same, that would be great!

@supratimdutta

You can use ‘contains()’ method.

@FindBy(how=How.XPATH,using=("//android.widget.TextView[contains(text(),‘Family’)]")) 
WebElement Family_Profile;

For text property, use text() than @text

@FindBy(how=How.XPATH,using=(“//android.widget.TextView[text()=‘Family Profile’]”))
WebElement Family_Profile;

If you still have some problem prefer using text property with contains like below:
@FindBy(how=How.XPATH,using=(“//android.widget.TextView[contains(.,‘Family Profile’)]”))

or

@FindBy(how=How.XPATH,using=(“//android.widget.TextView[contains(text(),‘Family Profile’)]”))
WebElement Family_Profile;

Let us know if any of the above didnt work out.

but best and fastest for android is native method:

String text = "your_text";
List<WebDriver> elementList = ((AndroidDriver) driver).
                findElements(MobileBy.AndroidUIAutomator("new UiSelector().textContains(\"" + text + "\")"));
// or
@AndroidFindBy(uiAutomator = "new UiSelector().textContains(\" your_text \")")
1 Like

I tried this, this does not work:

Hello Aleksei,
This also does not help:

Code used: @AndroidFindBy(uiAutomator = “new UiSelector().textContains(” Family “)”) WebElement Family_Android_Findby;

Your search text in code has blank. Maybe this issue. If not it means there is no element which has this text as value. Sometimes it goes to another attribute. Check with inspector where your search text is.

hi @supratimdutta,

i think you should try this: -
driver.findElementByAndroidUIAutomator(“new UiSelector().textContains(“Family”).textContains(“Profile”)”).click();

Hope so, it will work for you.

I tried but didn’t work. Using Appium 1.15.1
@AndroidFindBy(uiAutomator = “new UiSelector().textContains(‘Introducing Family’)”)
public MobileElement syncText;
Please let me know what’s wrong in this.

1 Like

Try again but with \" instead of '.

So it will be:

@AndroidFindBy(uiAutomator = "new UiSelector().textContains(\"Introducing Family\")")
public MobileElement syncText;
1 Like

Yaaaa it worked. Thanks u Sir.

It was my mistake. Sorry for this.

Hi, i have the same probem and i tried your solution, but it works only with webElement in web View.
Is it normal? What can i use for android element?