Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

Using selenium java i want to get all the values of a specified attribute within the current page
exemple :

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

how i get the result : value1, value2, value3 to store them in a list of strings

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can get a list of web elements with attribute my_attribute and then iterate over that list to collect all the attribute values.
Something like this:

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading