Reputation: 9
I am automating a mobile application through appium integrated with selenium web driver+testng. Now the problem i am facing is that i have list of tasks against which each task i have a link with same name (i.e: VIEW), same class name (i.e android.widget.TextView). Now i want to click on this specific 'VIEW' link for which i am passing the task name. I am using the following code but it is giving java.lang.IndexOutOfBoundsException: Index: 6, Size: 3 error.
List<WebElement> list = driver.findElements(By.xpath("//android.widget.TextView[@enabled='true']")); //returing all the tasks
for(int i = 0 ; i< list.size() ; i++){
String message1 = list.get(i).getText();
//System.out.println(message1);
if(message1.contains(ac+"-"+cnic)){
Thread.sleep(10000);
driver.findElements(By.xpath("//android.widget.TextView[@text='VIEW']")).get(i).click();
}
}
Upvotes: 1
Views: 1897
Reputation: 17563
You can use foreach loop instead of regular loop
for (WebElement element : list ) {
String message1 = element.getText();
//System.out.println(message1);
if(message1.contains(ac+"-"+cnic)){
Thread.sleep(10000);
element.click();
}
}
Upvotes: 1
Reputation: 768
You're looping on 'all the tasks', and then use that indice to get an element in a different list, the list of tasks with name 'VIEW'. You should loop directly on the tasks with the name view, if I understood correctly. Also, you can use a foreach loop like Shubham said, or in java 8 a stream with a foreach().
Upvotes: 2