C# System.NotSupportedException:&x27;集合是只读的;从iList中删除对象时引发

C# System.NotSupportedException:&x27;集合是只读的;从iList中删除对象时引发,c#,list,selenium-webdriver,C#,List,Selenium Webdriver,我在运行下面的代码片段时遇到一个异常 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: " +

我在运行下面的代码片段时遇到一个异常

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

            }

        }
我有一个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表行;
tableRows=FindElement(By.XPath(“//div[@ui grid='vm.gridwood'])).FindElements(By.XPath(“//div[@role='row']));
WriteLine(“循环前的表行计数:”+tableRows.count);
foreach(表行中的变量行){
if(row.Text.ToString()包含(“WSC”))
{
tableRows.Remove(row);//此处引发异常
}
}

感谢您提供的任何帮助

并非所有Implements IList的内容都是可编辑的。最简单的解决方案就是使用Linq构建一个过滤器并在其上执行ToList()

IList表行;
tableRows=FindElement(By.XPath(“//div[@ui grid='vm.gridwood'])).FindElements(By.XPath(“//div[@role='row']));
WriteLine(“循环前的表行计数:”+tableRows.count);
tableRows=tableRows.Where(row=>!row.Text.ToString().Contains(“WSC”)).ToList();

使用
foreach
循环时,不要修改集合。尝试使用
for
类循环。或者找到要删除的项目,保存对它的引用(LINQ可以在这里提供帮助),然后在循环后删除它。@Jonesopolis我如何实现上述目标?如果该web元素包含我不想要的字符串,则删除该web元素?好的,不管怎样,我想我找到了。我是否要创建另一个包含我不想要的元素的iList,然后使用第二个iList从原始iList中删除对象?可能是重复的哦,哇,这正是我需要的。谢谢你,斯科特Chamberlain@kevin如果正确回答了您的问题,请确保答案被接受。@JohnM.Wright很抱歉,我已经标记了它