我使用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