Yoi*_*iku 14 c# selenium readonly-collection listiterator
我正在使用selenium,我正在使用函数FindElements,所以我得到一个实现IReadOnlyCollection接口的元素.我想迭代列表,但似乎IReadOnlyCollection没有任何方法,如Get(int index)或操作[]的实现.
我想避免将结果转换为List或数组,因为我只想访问元素来读取它们.
目前我不想使用foreach,因为我需要管理索引,所以我可以将这些元素添加到另一个数组.
这就是我想要做的:
public void fillMatrix(){
IReadOnlyCollection<IWebElement> rows = Driver.FindElements(By.XPath("./*/tr"));
IReadOnlyCollection<IWebElement> elements;
matrix = new IControl[rows.Count()][];
for(int i = 0; i < matrix.Count(); ++i){
matrix[i] = rows[i].FinElements("./td").toArray();
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
小智 27
将ElementAt(int)函数与索引值一起使用.
这是到ElementAt(int)函数的MSDN链接https://msdn.microsoft.com/en-us/library/bb299233(v=vs.110).aspx
jjc*_*pek 11
我没有使用只读集合,但是从MSDN文档中,看起来可以使用ElementAt
方法获取给定索引处的元素.它应该像那样工作:
IReadOnlyCollection<IWebElement> rows = Driver.FindElements(By.XPath("./*/tr"));
int index = 1; // sample
var row = rows.ElementAt(index)
Run Code Online (Sandbox Code Playgroud)
您可能需要using System.Linq;
在类中添加语句,因为它ElementAt()
是Linq提供的扩展方法.
归档时间: |
|
查看次数: |
11347 次 |
最近记录: |