Reputation: 183
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.
Upvotes: 6
Views: 23363
Reputation: 66
In xPath 2 if you use Appium inspector with python you can use
//*[contains(@text, 'Song Name')]
that will only search for the text that contains the song name and it doesn't have to be exact same value. example :
els1 = driver.find_elements(by=AppiumBy.XPATH, value="//*[contains(@text, 'Song Name' )]")
if you want to use for the exact text value you can use
//*[@text= 'Song Name']"
Upvotes: 1
Reputation: 77
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;
}
}
Upvotes: 2
Reputation: 183
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.
Upvotes: 6
Reputation: 578
If @bsk answer is not working. It might be because of a new line break...
Could you try this to verify?
By.xpath("//android.widget.TextView[contains(@text, 'Ishq De Fanniyar (Female)')]");
Also is not a bad practice to use @resource-id too...
By.xpath("//android.widget.TextView[@resource-id='com.xefyr.ridez:id/tv_song_title' and contains(@text, 'Ishq De Fanniyar (Female)')]");
Upvotes: 0
Reputation: 215
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+"']");
Upvotes: 5