I am new to Appium automation and I'm trying to retrieve 'index' node value by using .getAttribute in my code but am not able to.
Is there any way that i can retrieve index of a particular element locator?
String x = driver.findElement(By.xpath("//android.widget.TextView[@text='"+str+"']/../..")).getAttribute("index");
You cannot retrieve index value using getAttribute()
method, instead use something like a counter to identify the number of the times the text field occurs on the screen.
By mySelector = By.xpath("//android.widget.TextView");
List<WebElement> myElements = driver.findElements(mySelector);
int count = 0;
for(WebElement e : myElements) {
count++;
if(e.getText().equals(str1)) {
System.out.println(count); //This will give the index value
}
else{
//do something else
}
}
To extract the index attribute from the node you can write a function()
as follows :
public void viewIndex(String str)
{
String x = driver.findElement(By.xpath("//android.widget.TextView[.='" + str + "']")).getAttribute("index");
}
Now, from your main()
or @Test
annotated Class call the function viewIndex()
with the intended text
as follows :
viewIndex("India Gate");