I’ve tried to get child element from list of elements and instead of
return child element value for each item in list -> it returns only
first item.
List<WebElement> allAccoElements = driver.findElements(By.xpath("//ul[@id='ListerContainer']//li[@class='lister-item']//div[@class='lister-item-content']"));
// Found 10 items
for (WebElement element: allAccoElements){
System.out.println(element.findElement(By.xpath("//img[@class='image-base']")).getAttribute("id"));
//For loop will print "id" of first element 10 times, why I can't to get access to other Elements in list?
}
Print always return id of first element in list, can anyone suggest me, how I can find child element of each element in list?
Instead, if I use the following code like workaround, all works fine.
List<WebElement> allAccoElements = driver.findElements(By.xpath("//ul[@id='ListerContainer']//li[@class='lister-item']//div[@class='lister-item-content']//img[@class='image-base']"));
// Found 10 items:
for (WebElement element: allAccoElements){
System.out.println(element.getAttribute("id"));
//Print 10 times with different id
}
Thanks to p0deje we’ve found the answer:
To find corresponding item inside other, we have to workaround it (by adding dot “.” before “//” in xpath).