【问题标题】:C# get first row from a dynamic tableC#从动态表中获取第一行
【发布时间】:2021-03-29 19:57:18
【问题描述】:

我创建了这个方法来从动态表中获取数据。但是,每次调用此方法时,我只想获得第一行。我找不到解决办法

基本上在下面的代码中,我想返回结果的tablerow[0]

    public static Dictionary<string, IWebElement> GetlatestResults(String GridPath, String GridPathHeader)
        {
            var Result = new Dictionary<string, IWebElement>();
           
            var workflowtable = DriverContext.Driver.FindElement(By.XPath(GridPath));
            IList<IWebElement> tableRow = workflowtable.FindElements(By.TagName("tr"));

            var workflowTableHeader = DriverContext.Driver.FindElement(By.XPath(GridPathHeader));
            IList<IWebElement> colElements = workflowTableHeader.FindElements(By.TagName("th"));
            
            //int rowIndex = 0;
            foreach (var row in tableRow)
            {
                int headerValueIndex = 0;
                IList<IWebElement> rowValues = row.FindElements(By.TagName("td"));

                foreach (var rowValue in rowValues)
                {
                  
                    Result.Add(colElements[headerValueIndex].Text, rowValue);

                    headerValueIndex++;
                }

               //rowIndex++;
            };

            return Result;
        }

【问题讨论】:

  • IWebElement row0 = tableRow[0];

标签: c# .net automation


【解决方案1】:

试试这个

var row = tableRow.First();
int headerValueIndex = 0;
IList<IWebElement> rowValues = row.FindElements(By.TagName("td"));

foreach (var rowValue in rowValues)
{
                  
     Result.Add(colElements[headerValueIndex].Text, rowValue);

     headerValueIndex++;
 }

完全跳过foreach

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    相关资源
    最近更新 更多