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

狂风中的少年 提交于 2021-02-05 07:49:51

问题


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:


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:

  • How to extract the dynamic values of the id attributes of the table elements using Selenium and Java
  • How to print all the button texts within the url using Selenium through Java


来源:https://stackoverflow.com/questions/57520786/cannot-cast-from-listfunctions-function1object-string-to-liststring-creati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!