I have a list of elements, located in Selenium with the By.CssSelector
method:
var contentRows = new List<TableRow>();
for (var i = 1; i < PositiveInfinity; i++)
{
var cssSelectorToFind = $"tbody > tr:nth-child({i})";
var bySelector = By.CssSelector(cssSelectorToFind);
var rowElement = WebElement.FindElements(bySelector).ToArray();
if (rowElement.Length == 1)
{
var description = $"{Description} Content row: {i}. Selected by: {bySelector}.";
var tableRow = new TableRow(bySelector, WebDriver, description, Headers);
contentRows.Add(tableRow);
}
else
{
if (rowElement.Length == 0)
{
break;
}
else
{
throw new InvalidOperationException($"The selector {bySelector} returned more that one row at the same ordinal position. Should be impossible... Best look at the selector and the HTML.");
}
}
}
return contentRows;
For each of these rows I need to set a class in the HTML of selected
.
I understand that I have to do this with the JavaScriptExecutor
.
- Is there a way to get a reference to each of these so I can add this individually?
- Is the only way to give each row some unique ID and then use that in the JavaScrit?