Cannot get 'index' node detail by getAttribute

爱⌒轻易说出口 提交于 2019-12-13 05:11:25

问题


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");

回答1:


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
    }
}



回答2:


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");


来源:https://stackoverflow.com/questions/48823391/cannot-get-index-node-detail-by-getattribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!