Cannot cast from List<Functions.Function1<Object,String>> to List<String> creating a list from list of WebElements using Selenium and stream() Java8
Asked Answered
C

1

1

I'm trying to read an HTML table and get all the values into a 2d list using selenium. I'm using streamable to create a List inside another map() method. But I get Cannot cast from List<Functions.Function1<Object,String>> to List<String> error at nested collect method's line

HTML:

+--------+-------+
| r1 v1  | r1 v2 |
+--------+-------+
| r2 v1  | r2 v2 |
+--------+-------+

<html>
    <body>
        <table>
            <tbody>
                <tr>
                    <td>
                        <div>
                            <span>r1 v1</span>
                        </div>
                    </td>
                    <td>
                        <div>
                            <span>r1 v2</span>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div>
                            <span>r2 v1</span>
                        </div>
                    </td>
                    <td>
                        <div>
                            <span>r2 v2</span>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

Java Code:

public List getTableData() {
    return webElement
            .findElements(By.xpath(".//table/tbody/tr"))
            .stream()
            .map(row -> {
                return row
                    .findElements(By.xpath(".//td/div/span"))
                    .stream()
                    .map(cell -> {
                        return cell
                            .getAttribute("innerText");
                    })
                    .collect(Collectors.toList()); // : Cannot cast from List<Functions.Function1<Object,String>> to List<String>
            })
            .collect(Collectors.toList());
}

Why am I gettting this error? How can I create a 2d List out of this table using java streamable?

Chocolate answered 16/8, 2019 at 7:50 Comment(0)
L
1

To create a List with the innerText using Java8's stream() and map() you can use the following solution:

  • cssSelector:

    List<String> myTexts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("table>tbody tr td span"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList());
    
  • xpath:

    List<String> myTexts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//table/tbody//tr//td//span"))).stream().map(element->element.getAttribute("innerHTML")).collect(Collectors.toList());
    

References: You can find a couple of relevant discussions in:

Lagging answered 16/8, 2019 at 10:30 Comment(1)
This way it's creating an array of values right? Right now I'm using for each parent loop and streamable as the inner loopChocolate

© 2022 - 2024 — McMap. All rights reserved.