[英]C# Selenium: Can't catch exception when using wait.Until
When using wait.Until()
in a try / catch block, if the element isn't there it throws NoSuchElementException exception.在 try / catch 块中使用wait.Until()
时,如果元素不存在,则会引发 NoSuchElementException 异常。 However, when I try to catch the exception, I still get the error.但是,当我尝试捕获异常时,仍然会收到错误消息。
try
{
wait.Until(el => el.FindElement(By.Id("courseTestItem")));
driver.Close();
driver.SwitchTo().Window(driver.WindowHandles.Last());
Console.WriteLine("Skipped: " + courses[0].Item1.Text + " (Has test)");
}
catch (OpenQA.Selenium.NoSuchElementException) { }
I even tried just using a catch (Exception e) { }
and that still didn't catch the error.我什至尝试只使用catch (Exception e) { }
并且仍然没有捕获错误。
Instead of using a try / catch use:而不是使用 try / catch 使用:
if (wait.Until(el => el.FindElements(By.Id("filterText"))).Count > 0) { }
else if (wait.Until(el => el.FindElements(By.Id("sp-search-results-search"))).Count > 0) { }
else System.Threading.Thread.Sleep(5000);
This checks to see if more than 0 elements matching the criteria is found, meaning that the element was found.这将检查是否找到超过 0 个符合条件的元素,这意味着找到了该元素。
Thanks to u/Simmo7 on Reddit for the help.感谢 Reddit 上的 u/Simmo7 的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.