首页 > 解决方案 > C#从动态表中获取第一行

问题描述

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

基本上在下面的代码中,我想返回结果的 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;
        }

标签: c#.netautomation

解决方案


尝试这个

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


推荐阅读