I realized only in Catch to change the variable and using goto repeat a part of the code, but how to do it correctly?
int kolVSpiske = list.Count - 1; IWebElement elem = null; povtor: try { elem = driver.FindElement(By.XPath("//*[.='" + list.ElementAt(kolVSpiske) + "'][last()]")); } catch (InvalidSelectorException) { elem = driver.FindElement(By.XPath("//*[.=concat('" + XpathEncode(list.ElementAt(kolVSpiske)) + "')][last()]")); } catch (NoSuchElementException) { kolVSpiske -= 1; goto povtor; } UPD:
Based on the comments, on the issue everything seemed to be clear, i.e. need a cycle and in case of success - leave it.
But it's not entirely clear why goto bad. After all, in my case, he is doing the same thing as the cycle would have done - no?
And so the code will get an exception - when the list exhausted.
UPD:
Without realizing it, it turned out that with the question about the goto , I touched the eternal question. After reading about 10 different discussions about it and here's an article:
Habr - GOTO or not GOTO that is the question
- for myself all the questions on it closed.