我使用C#.NET 4和Selenium WebDriver 2.44.0.0和chromeDriver。 当它无法找到元素,硒抛出错误:C#如何从NoSuchElementException获取方法和选择器
no such element
(Session info: chrome=38.0.2125.104)
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64)
但我想知道哪些元素是缺少。我看到一些文章说,它可以显示这样的细节:
OpenQA.Selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"hello"}
谁能告诉我怎么去从NoSuchElementException异常的方法和选择?
这里是我的代码
try
{
for(int i=0; i<10; i++)
{
string className = "items-" + i;
IWebElement t = Driver.FindElement(By.CssSelector("[class$='" + className + "'] > span"));
t.Click();
}
}
catch (NoSuchElementException ex)
{
Logger.Error("Error: " + ex.Message);
Debug.WriteLine(ex.Message);
}
你真的应该避免“例外编码”。例外情况应该是特例 - 例如硬盘空间不足或网络连接失败。如果你正在编写代码,并希望你的代码能够“正常”工作,那么你做错了什么。 – Enigmativity 2014-10-31 06:42:17