C# Selenium webdriver系统无效强制转换异常

C# Selenium webdriver系统无效强制转换异常,c#,selenium,C#,Selenium,为了在C#中为神经网络收集一些测试数据,我想使用Selenium来获取一些动态生成的数据。Selenium站点上有一个示例实现,它似乎正是我所需要的。该示例在标记名上搜索,我在类名上搜索,但除此之外,我认为它是相同的。 但是,当我运行此代码时,使用IWebElements创建IList可以工作,但以下IJavaScriptExecutor抛出无效的强制转换异常: 无法强制转换类型为的对象 System.Collections.ObjectModel.ReadOnlyCollection 1[Sy

为了在C#中为神经网络收集一些测试数据,我想使用Selenium来获取一些动态生成的数据。Selenium站点上有一个示例实现,它似乎正是我所需要的。该示例在
标记名
上搜索,我在
类名
上搜索,但除此之外,我认为它是相同的。 但是,当我运行此代码时,使用
IWebElements
创建
IList
可以工作,但以下
IJavaScriptExecutor
抛出无效的强制转换异常:

无法强制转换类型为的对象 System.Collections.ObjectModel.ReadOnlyCollection 1[System.Object] 打字 System.Collections.Generic.IList 1[OpenQA.Selenium.IWebElement]

这里有一些代码,这是“文本”的代码,我对“num”也这样做:

//按类名查找元素
IList labels=driver.FindElements(By.ClassName(“text”);
//获取每个类标签的所有输入元素
IList labVals=(IList)((IJavaScriptExecutor)驱动程序)。ExecuteScript(
var labels=arguments[0],labVals=[];for(var i=0;i
我看过这个问题,它可能指向同一个问题,但我看不出提供的答案对我有什么帮助

一种选择是将
IJavaScriptExecutor
语句分解为带有变通方法的“离散”代码,但我不知道如何做到这一点


一旦我在
列表结构中有了文本标签和数据值,我应该能够找到我需要的数字。

这不是使用javascript,但它可以工作。 我将使用一个CssSelector方法来接收您需要的列/行的throught参数,然后您将使用循环调用此方法以从页面获取所有信息

检查页面的css这是我从第一列/行得到的

table.mdcTable > tbody > tr:nth-of-type(3) > td:nth-of-type(1)
因此,数字“3”与第一行相关,“1”是第一列。因此,我们可以创建一个方法来返回您想要的确切元素:

public IWebElement test(int line, int row)
    {
        return driver.FindElement(By.CssSelector(string.Format("table.mdcTable > tbody > tr:nth-of-type({}) > td:nth-of-type({})", line + 2, row)));
    }
调用此方法将返回包含文本的元素,因此您需要做的就是使用“element.text”来指定“cell”的值,或者使该方法直接返回文本

public String test(int line, int row)
    {
        return driver.FindElement(By.CssSelector(string.Format("table.mdcTable > tbody > tr:nth-of-type({}) > td:nth-of-type({})", line + 2, row))).Text;
    }
唯一的问题是“Latest”列,因为它们不仅包含数字,还包含一个条。您必须创建一个只处理这些列的方法

这将以如下方式结束:

            try
            {
                int line = 1;
                int column = 1;
                while(column <= 7)
                    valueOfTheCell = test(line, column);
                getLatestGreen(line); //string.Format("tbody > tr:nth-of-type({0}) > td:nth-of-type(9) > span.text", line)
                getLatestRed(line);  //string.Format("tbody > tr:nth-of-type({0}) > td:nth-of-type(8) > span.text > b", line)
            }
            catch (NoSuchElementException)
            {
                //Exception will be thrown when the code reaches the end of the list
            }
试试看
{
内线=1;
int列=1;

虽然(列之所以发生强制转换错误,是因为IJavascriptExecutor输出了常规的
System.Object
类,然后我尝试将该类强制转换为
IWebElement
。这在某些情况下可能有效,但在这种情况下无效。将接收的
IList
更改为
IList
可解决强制转换异常。通过此操作,代码将运行。),然后我通过调试器发现所有数据都是通过
标签
列表中的第一部分代码捕获的。
IJavaScriptExecutor
只返回空项。因此在我的情况下不需要第二步。

@Wermuth:感谢这种替代方法。看起来很有趣,因为它似乎在没有ext的情况下工作我会试试的。
            try
            {
                int line = 1;
                int column = 1;
                while(column <= 7)
                    valueOfTheCell = test(line, column);
                getLatestGreen(line); //string.Format("tbody > tr:nth-of-type({0}) > td:nth-of-type(9) > span.text", line)
                getLatestRed(line);  //string.Format("tbody > tr:nth-of-type({0}) > td:nth-of-type(8) > span.text > b", line)
            }
            catch (NoSuchElementException)
            {
                //Exception will be thrown when the code reaches the end of the list
            }