public void RemoveAllEmployees(string employee) { WaitForSeconds(2); WaitForElementToAppear(lastEmployee, 3); int i; int n = 1; int total; do { total = totalEmployees.Count; for (i = n; i <= total + 1; i++) { try { IWebElement emp = _driver.FindElement(By.XPath($"//ul[@id='employee-list']/li[{i}]")); if (emp.Text == employee) { ProceedWithDelete(emp); break; } } catch (NoSuchElementException) { IWebElement emp = _driver.FindElement(By.XPath($"//ul[@id='employee-list']/li[{--i}]")); if (emp.Text == employee) { ProceedWithDelete(emp); break; } else if (i == total) { break; } } } }while (i < total); void ProceedWithDelete(IWebElement emp) { Actions action = new Actions(_driver); action.DoubleClick(emp).Perform(); BaseTest baseTest = new BaseTest(_driver); WaitForSeconds(2); deleteBtn.Click(); WaitForSeconds(2); baseTest.AcceptAlert(); WaitForElementToAppear(lastEmployee, 3); WaitForSeconds(3); n = i; } }