1
我试图摆脱的元素列表子元素和替代 在列表中每个项目的回报子元素的值 - >它只返回 第一项。的webdriver获取元件与元件阻挡
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
}
这是因为您在'findElement'中使用了XPath选择器,它以'/'开头(它指向HTML的根,因此不适用于'element'的上下文)。 – p0deje 2012-04-09 09:12:06
事实上,要在其他地方找到相应的项目,我们必须解决它(通过在xpath中的“//”之前添加点“。”): 谢谢p0deje您的建议! – 2012-04-09 09:28:49
很高兴帮助。随意+1评论和回答这个问题,所以其他人可以找到解决方案。 – p0deje 2012-04-09 10:44:36