0

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?
BanksySan
  • 27,362
  • 33
  • 117
  • 216

1 Answers1

1

You can pass IWebElement references as parameters to ExecuteScript, and then work with those in your JavaScript, if that's what you're looking for. So for example, if you have an IWebElement and want to highlight it by drawing a red border around it, you can do that with the following code (based on the answers to this question:

var element = driver.FindElement(By.Name("..."));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.border='3px solid red'", element);
Community
  • 1
  • 1
ralph.mayr
  • 1,320
  • 8
  • 12