'Selenium c#: not able to find Xpath with id generated

i am working with a table and when one name is found on the table: i want to click corresponding delete button.

IList<IWebElement> CtrlRow = option.FindElements(By.TagName("td"));
                    for (int CountRows = 0; CountRows < CtrlRow.Count; CountRows++)
                { 
                        IWebElement row = CtrlRow[CountRows];
                    Console.WriteLine("Found content: " + row.Text.ToString());
                    if (row.Text.ToString().Equals(t))

                    {
                        int myElementRowNumber = CtrlRow.IndexOf(row);
                        var ty = @"(//td)["+ myElementRowNumber + "]";
                        var DeleteRow = @"//tbody/" + "tr["  + myElementRowNumber + "]" + "/td[6]/a[1] ";
                   var tt =  row.FindElement(By.XPath(ty));
                        Console.WriteLine("Found Controller ID in System List: " + t);
                        try
                        {                                  
                            row.FindElement(By.XPath(DeleteRow)).Click();
                     

here, index of row is not coming correctly as corresponding Delete button. what i have done: 1:tried manipulating index of delete as row index: doent work 2: trying to get xpath of the row which have my element name , so that copy the right index(in Xpath, index is right), and manipulate Delete xpath: cant get Xpath in ID of the row.

i am working with Selenium with c#.



Solution 1:[1]

The problem is in this line:

var ty = @"(//td)["+ myElementRowNumber + "]";

You are not using any ID attribute, change to this one:

var ty = "(//td)["+ "@ID='" + myElementRowNumber + "']";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tal Angel