0

I'm using Selenium in C#, to try and get the values from a table on the web into a local table.

I get all the rows using:

var allTableRows = driver.FindElements(By.ClassName("table-row"));

I can see that allTableRows has multiple different rows, differing by ID and text. But then, when I try to iterate over them, and gather data for each row with:

foreach (var row in allTableRows)
{
    var currentChange = new ChangeHistoryRow();
    var date = row.FindElements(By.XPath("//cell"))[0]?.Text;

    if (date != null)
    {
        currentChange.Date = date;
    }

    var time = row.FindElements(By.XPath("//cell"))[1]?.Text;

    if (time != null)
    {
        currentChange.Time = time;
    }
    
    listOfChanges.Add(currentChange);
}

Every item in my final list has the same data, the ones from the first row. (Even though the original table had different date/time values in each row)

There are no additional selectors to be used like specific id/class, so I am forced to use general rules (rows by class name, and then cells in each row only differing in [0], [1], [2]...etc)

I'm not sure why the data always points to the first row, since we are trying to get elements in a foreach, for that specific row.

4

1 回答 1

1
 var date = row.FindElements(By.XPath(".//cell"))[0]?.Text;

利用 '。' 在定位器的前面,否则上下文将来自 root 。当你给'。它从父元素搜索

于 2021-03-04T00:20:30.040 回答