Selenium C# Tutorial: How to Handle NoSuchElement Exception

Updated: Oct 19, 2021

In this article, I going to show you how to handle a very common scenario of test executions that can fail if the element is not found and how can us handle the NoSuchElement Exception.


To simulate this scenario, we will run this code with the wrong Xpath value:


class Program

{

static void Main(string[] args)

{

//string XPath = "//*[@id=\"input_comp - kf5t63ji\"]";

string cookieApproval = "/html/body/div[2]/div/div[2]/button[2]";

string searchField = "DD/html/body/div[4]/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/div/div[4]/div[2]/div/aside/div/div[1]/div[1]/div/form/table/tbody/tr/td[1]/input";

string searchButton = "/html/body/div[4]/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/div/div[4]/div[2]/div/aside/div/div[1]/div[1]/div/form/table/tbody/tr/td[2]/input";

IWebDriver firefoxDriver = new FirefoxDriver();

TEstPreperation(firefoxDriver, cookieApproval);


IWebElement xpath_searchField = firefoxDriver.FindElement(By.XPath(searchField));

IWebElement xpath_searchButton = firefoxDriver.FindElement(By.XPath(searchButton));

XPathSearch(xpath_searchField, xpath_searchButton);

// driver.Quit();

}


private static void TEstPreperation(IWebDriver webDriver ,string cookie)

{

webDriver.Navigate().GoToUrl("http://www.machtested.com/");

Thread.Sleep(15000);

webDriver.FindElement(By.Id("fclose-button")).Click();

//webDriver.FindElement(By.XPath(cookie)).Click();

}


private static void XPathSearch(IWebElement searchField, IWebElement searchButton)

{

if (searchField.Displayed)

{

searchField.SendKeys("Quality");

Thread.Sleep(3000);

searchButton.SendKeys(Keys.Enter);

}

else

{

Console.WriteLine("Element is NOT found");

}

}

}


Result:

As you can see, we received a NoSuchElement Exception that will crash the entire code execution. To Handle this situation, we will need to ensure that the code contains a "Try/Catch" block that will handle such errors.


Here is how we do it:


class Program

{

static void Main(string[] args)

{

string cookieApproval = "/html/body/div[2]/div/div[2]/button[2]";

string searchField = "dd/html/body/div[4]/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/div/div[4]/div[2]/div/aside/div/div[1]/div[1]/div/form/table/tbody/tr/td[1]/input";

string searchButton = "/html/body/div[4]/div[2]/div[2]/div[2]/div[2]/div[2]/div[2]/div/div[4]/div[2]/div/aside/div/div[1]/div[1]/div/form/table/tbody/tr/td[2]/input";

IWebDriver firefoxDriver = new FirefoxDriver();

TEstPreperation(firefoxDriver, cookieApproval);

XPathSearch(firefoxDriver, searchField, searchButton);

// driver.Quit();

}

private static void TEstPreperation(IWebDriver webDriver ,string cookie)

{

webDriver.Navigate().GoToUrl("http://www.machtested.com/");

Thread.Sleep(15000);

webDriver.FindElement(By.Id("fclose-button")).Click();

//webDriver.FindElement(By.XPath(cookie)).Click();

}


public static void XPathSearch(IWebDriver driver,string xpathForSearchField, string xpathForsearchButton)

{

try

{

IWebElement xpathsearchFieldElement = driver.FindElement(By.XPath(xpathForSearchField));

IWebElement searchButtonElement = driver.FindElement(By.XPath(xpathForsearchButton));

if (xpathsearchFieldElement.Displayed)

{

xpathsearchFieldElement.SendKeys("Quality");

Thread.Sleep(3000);

searchButtonElement.SendKeys(Keys.Enter);

}

}

catch (Exception)

{

Console.WriteLine("Element is NOT found");

}

}

}

Result:



212 views0 comments