检索列表中指定属性名称的所有值?

retreiving all values of a specified attribute name in a list?

使用 selenium java 我想获取当前页面中指定属性的所有值 例子:

<div my_attribute="value1">
<div my_attribute="value2">
<div my_attribute="value3">

我如何获得结果:value1、value2、value3 以将它们存储在字符串列表中

您可以获得具有属性 my_attribute 的网络元素列表,然后遍历该列表以收集所有属性值。
像这样:

List<WebElement> list = driver.findElements(By.xpath("//div[@my_attribute]"));
List<String> values = new ArrayList<>();
for(WebElement el:list){
    String value = el.getAttribute("my_attribute");
    values.add(value);
}