Java and Selenium: Getting list contents when id’s and selectors are poorly written

You’re mixing up xPath and CSS. Below is the example how you can use CSS for fetching what you need:

List<WebElement> labels = driver.findElements(
        new ByChained(By.cssSelector("#residenceChoice"),
                      By.cssSelector(".hb-label-tekst")
        ));
labels.forEach(webElement -> {
    System.out.println("Label text: " + webElement.getText());
    System.out.println("Label 'for': " + webElement.getAttribute("for"));
});

And this is the exampel for xPath:

List<WebElement> labels = driver.findElements(By.xpath("//*[@id='residenceChoice']//label[@class='hb-label-tekst']"));
labels.forEach(webElement -> {
    System.out.println("Label text: " + webElement.getText());
    System.out.println("Label 'for': " + webElement.getAttribute("for"));
});

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top