【发布时间】:2021-11-24 19:03:53
【问题描述】:
有 5 个 Web 元素共享相同的 xpath。我想从每个元素中获取文本并将其存储在列表中。以下是我失败的尝试:
List<WebElement> ActualAdFormats_Elements = driver.findElements(By.xpath("(//h4[text()='Select Ad Format']/..//strong)"));
for(WebElement AdFormat:ActualAdFormats_Elements) {
ActualAdFormat_List.add(AdFormat.getText());
}
【问题讨论】:
-
它是如何失败的?你的代码的结果是什么?
-
结果如何?
-
ActualAdFormat_List 为 null 是我得到的。我以错误的方式定义它。按照 Cruise 提到的方式定义它并且它有效。谢谢!
标签: list selenium for-loop arraylist