运行下面的代码片段时抛出异常。
我有一个 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/