Reputation: 443
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.
Upvotes: 0
Views: 199
Reputation: 19979
var date = row.FindElements(By.XPath(".//cell"))[0]?.Text;
Use '.' infront of the locator else the context will be from root . When you give '.' it search from parent element
Upvotes: 1