如何通过索引访问IReadOnlyCollection的元素? [英] How can I access a element of a IReadOnlyCollection through it index?
问题描述
我正在使用硒,并且正在使用功能FindElements,所以我得到一个实现IReadOnlyCollection接口的元素.我想遍历列表,但似乎IReadOnlyCollection没有诸如Get(int index)或操作[]的实现之类的任何方法.
I am working with selenium and I am using the function FindElements so I am getting a element that implements IReadOnlyCollection interface. I want to iterate through the list but it seems that IReadOnlyCollection doesnt have any method like Get(int index) or a implementation of the operation [].
我想避免将结果转换为列表或数组,因为我只想访问元素以读取它们.
I want to avoid transforming the result to a List or to an array since I just want to access the elements to read them.
当前我不想使用foreach,因为我需要管理索引,以便可以将这些元素添加到另一个数组中.
Currently I don't want to use a foreach since I need to manage an index so I can add those elements to an another array.
这就是我想要做的:
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();
}
}
谢谢
推荐答案
使用ElementAt(int)函数和索引值.
Use the ElementAt(int) function with the index value.
这是到ElementAt(int)函数的MSDN链接 https://msdn.microsoft.com/zh-CN/library/bb299233(v=vs.110).aspx
Here is the MSDN link to the ElementAt(int) function https://msdn.microsoft.com/en-us/library/bb299233(v=vs.110).aspx
这篇关于如何通过索引访问IReadOnlyCollection的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!