c# - 系统不支持异常: 'Collection is read-only.' thrown when removing object from iList

标签 c# list selenium-webdriver

运行下面的代码片段时抛出异常。

我有一个 iListof webelements,如果该元素包含字符串“WSC”,我想将其从 iList 中删除。

谁能帮帮我吗?代码如下。

IList<IWebElement> tableRows;
        tableRows = FindElement(By.XPath("//div[@ui-grid='vm.gridWoodSmokeViolations']")).FindElements(By.XPath("//div[@role='row']"));
        Console.WriteLine("table row count before loop: " + tableRows.Count);
        foreach (var row in tableRows) {

            if (row.Text.ToString().Contains("WSC"))
            {

                tableRows.Remove(row); //exception thrown here

            }

        }

感谢您提供的任何帮助

最佳答案

并非所有实现 IList 的东西都是可编辑的。最简单的解决方案就是使用 Linq 构建一个过滤器并对其执行 ToList() 操作。

    IList<IWebElement> tableRows;
    tableRows = FindElement(By.XPath("//div[@ui-grid='vm.gridWoodSmokeViolations']")).FindElements(By.XPath("//div[@role='row']"));

    Console.WriteLine("table row count before loop: " + tableRows.Count);

    tableRows = tableRows.Where(row=> !row.Text.ToString().Contains("WSC")).ToList();

关于c# - 系统不支持异常: 'Collection is read-only.' thrown when removing object from iList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46187478/

相关文章:

c# - 如何获取具有特定主题标签值的注释节点

c# - Mysql 更新数据库 System.FormatException

c# - 在 WPF C# 中处理两个 UI 线程

c# - 使用ListView控件显示数据但加入一个引用表

python-3.x - 如何使用selenium python自动扫描二维码

从数据帧列表中返回平均值的数据帧

c# - 创建要在动态对象上运行的方法列表

python - 使用 Pygame,在不同位置绘制图像的副本

selenium - 当警报存在时打开新选项卡 Selenium?

c# - 实例化 EdgeDriver 时会发生异常