无法从List< Functions.Function1< Object,String>>中强制转换列出< String>使用Selenium和stream()Java8从WebElement列表创建列表 [英] Cannot cast from List<Functions.Function1<Object,String>> to List<String> creating a list from list of WebElements using Selenium and stream() Java8

查看:58
本文介绍了无法从List< Functions.Function1< Object,String>>中强制转换列出< String>使用Selenium和stream()Java8从WebElement列表创建列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取HTML表,并使用selenium将所有值放入2d列表中.我正在使用streamable在另一个map()方法内创建一个列表.但是在嵌套的collect方法行中出现Cannot cast from List<Functions.Function1<Object,String>> to List<String>错误

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

+--------+-------+
| 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代码:

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());
}

为什么我会收到此错误?如何使用java streamable在此表之外创建2d列表?

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

推荐答案

使用Java8的

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

    这篇关于无法从List&lt; Functions.Function1&lt; Object,String&gt;&gt;中强制转换列出&lt; String&gt;使用Selenium和stream()Java8从WebElement列表创建列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆