简体   繁体   English

我正在使用带有硒和C#的属性findelements,但是它始终给出相同的错误

[英]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 System.Collections.ObjectModel.ReadOnlyCollection`1 [OpenQA.Selenium.IWebElement]或其他相同

This is also shown in a datagridview, in her rows. 这也显示在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);     
}

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) 它是一个列表: <ul class="list-unstyled list-inline">... (考虑与网站链接有关的评论)

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存储收集的数据,这是非常不正确的。 DataGridView is used for viewing data in MVC application. DataGridView用于在MVC应用程序中查看数据。 There is no standard Selenium class for storing table data. 没有用于存储表数据的标准Selenium类。 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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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