[英]I'm using the property findelements with selenium and C#, but it keeps giving the same error
這是我試圖用來獲取各個元素的代碼的一部分,但它一直給我以下錯誤:
System.Collections.ObjectModel.ReadOnlyCollection`1 [OpenQA.Selenium.IWebElement]或其他相同
這也顯示在datagridview的行中。
IList<IWebElement> ruas = Gdriver.FindElements(By.ClassName("search-title"));
String[] AllText = new String[ruas.Count];
int i = 0;
foreach (IWebElement element in ruas)
{
AllText[i++] = element.Text;
table.Rows.Add(ruas);
}
第一件事是:據我了解,您所討論的元素未包含在表中。 它是一個列表: <ul class="list-unstyled list-inline">...
(考慮與網站鏈接有關的評論)
如果要查找這些元素,可以使用以下代碼:
var elements = driver.FindElements(By.CssSelector("ul.list-inline > li > a"));
// Here you can iterate though links and do whatever you want with them
foreach (var element in elements)
{
Console.WriteLine(element.Text);
}
// Here is the collection of links texts
var linkNames = elements.Select(e => e.Text).ToList();
考慮到您收到的錯誤,我可以假設您正在使用DataGridView存儲收集的數據,這是非常不正確的。 DataGridView用於在MVC應用程序中查看數據。 沒有用於存儲表數據的標准Selenium類。 有多種方法,但是我不建議您,因為我不知道您要達到的目標。
這是我如何回答自己的問題:
IList<string> all = new List<string>();
foreach (var element in Gdriver.FindElements(By.ClassName("search-title")))
{
all.Add(element.Text);
table.Rows.Add(element.Text);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.