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?

1 Answer 1

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:

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 loop
    – s1n7ax
    Aug 20, 2019 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.