My task involves working with a collection of elements in Selenium, specifically located using 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;
Now, I am looking to add a class of selected
to each of these rows in the HTML.
I have knowledge that this can be achieved using the JavaScriptExecutor
.
- Is there a method to access each row individually for applying this class?
- Do I need to assign a unique ID to each row and utilize it in JavaScript?