简体   繁体   中英

I'm using the property findelements with selenium and C#, but it keeps giving the same error

This is a part of the code that i was trying to use to get the respective elements, but it keeps giving me the following error:

System.Collections.ObjectModel.ReadOnlyCollection`1[OpenQA.Selenium.IWebElement]or others identical

This is also shown in a datagridview, in her rows.

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);     
}

First thing is: as far as I understand the elements you are talking about are not contained in table. Its a list: <ul class="list-unstyled list-inline">... (considering the comment you left with site link)

If you want to find those elements you can use the code below:

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();

Considering the error you get, I may assume that you are using DataGridView for storing collected data, which is terribly incorrect. DataGridView is used for viewing data in MVC application. There is no standard Selenium class for storing table data. There are multiple approaches for this, but I can't suggest you any because I don't know your what you are trying to achieve.

Here is how i answered my own question:

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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM