1

我试图从元素列表中获取子元素,而不是为列表中的每个项目返回子元素值 - >它只返回第一项。

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?

相反,如果我使用以下代码(如解决方法),一切正常。

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

1 回答 1

3

感谢 p0deje,我们找到了答案:

要在 other 中找到相应的项目,我们必须解决它(通过在 xpath 中的“//”之前添加点“.”)。

于 2013-01-14T18:20:26.700 回答