Selenium C# Tutorial: How to Identify Web Elements Using Selenium XPath (Advanced)
Updated: Mar 5
The XML path language (XPath) is a language that we can use for investigating specific nodes of an XML document based on specific search criteria.
Selenium WebDriver supports the XML path language as a strategy to locate elements using flexible XPath queries.
Also:
Not like the CSS strategy, XPath can identify the parent element using his chilled element.
XPath is the least preferable strategy to locate elements due to reduced performance compared to other strategies.
A single / slash at the beginning of the XPath instructs the XPath locator to search for elements starting from the root node.
A double // slash at the beginning of the XPath instructs the XPath locator to search the element anywhere.
Finding elements with absolute path
When this technique is used, we will need to deliver the specific location of the element in the DOM hierarchy. As a result, multiple dependencies in the DOM structure can affect the locator's capability to locate the element.
Scenario:
1. log-in to https://demowf.aspnetawesome.com/ .
2. Search for an object in the Autocomplete section

XPath:
string XPath = "//*[@id=\"ContentPlaceHolder1_Meal\"]";
Code
class Program
{
static void Main(string[] args)
{
string XPath = "//*[@id=\"ContentPlaceHolder1_Meal\"]";
string cookieApproval = "html body.device-windows div._2svEM._2f7c5._4Ck7e div.HzPhN div._1STS6 button.undefined.MWTAz._6WUOF";
IWebDriver firefoxDriver = new FirefoxDriver();
TEstPreperation(firefoxDriver, cookieApproval);
IWebElement xpathElement = firefoxDriver.FindElement(By.XPath(XPath));
XPathSearch(xpathElement);
// driver.Quit();
}
private static void TEstPreperation(IWebDriver webDriver ,string cookie)
{
webDriver.Navigate().GoToUrl("https://demowf.aspnetawesome.com/");
Thread.Sleep(3000);
//webDriver.FindElement(By.CssSelector(cookie)).Click();
//Thread.Sleep(3000);
}
private static void XPathSearch(IWebElement XPathElement)
{
if (cssElement.Displayed)
{
XPathElement.SendKeys("Element is available");
}
else
{
Console.WriteLine("Element is NOT found");
}
}
Finding elements with absolute path
Using the element relative path, we can locate the element directly without providing the whole location in the DOM.
Besides, many elements may be matched to the search query when using a relative path. Therefore it will return the first element found in the DOM.
Scenario:
1. log-in to https://demowf.aspnetawesome.com/ .
2. Mark the "Nuts" checkbox using a relative path
Full XPath:
string XPath = "//*[@id=\"maincont\"]/div[1]/div[4]/div[1]/div[2]/div/ul/li[4]/label/div[1]";
Relative Path:
string XPath = "//div/ul/li[4]/label/div[1]";

Code:
class Program
{
static void Main(string[] args)
{
string XPath = "//div/ul/li[4]/label/div[1]";
string cookieApproval = "html body.device-windows div._2svEM._2f7c5._4Ck7e div.HzPhN div._1STS6 button.undefined.MWTAz._6WUOF";
IWebDriver firefoxDriver = new FirefoxDriver();
TEstPreperation(firefoxDriver, cookieApproval);
IWebElement xpathElement = firefoxDriver.FindElement(By.XPath(XPath));
XPathSearch(xpathElement);
// driver.Quit();
}
private static void TEstPreperation(IWebDriver webDriver ,string cookie)
{
webDriver.Navigate().GoToUrl("https://demowf.aspnetawesome.com/");
Thread.Sleep(3000);
//webDriver.FindElement(By.CssSelector(cookie)).Click();
//Thread.Sleep(3000);
}
private static void XPathSearch(IWebElement XPathElement)
{
if (XPathElement.Displayed)
{
XPathElement.SendKeys("Element is available");
}
else
{
Console.WriteLine("Element is NOT found");
}
}
Finding elements based on an index
To overcome the "First element returned" issue that we saw in the previous example, we can search the element via the Index used as another filter.
For example:
Search via full XPath will look like this: //[@id="maincont"]/div[1]/div[4]/div[1]/div[2]/div/ul/li[4]/label/div[1]
Search Via Index will look like this:
"//li[4]/label/div[1]";
Finding elements based on attributes
We can locate elements using their attributes in XPath (Similar to what we saw in the CSS locator).
We will search for the input element (In that case it’s the name field) with the following attributes:
ID attributes:
WebElement = Firefox.FindElement(By.XPath("//input[@id='ContactForm1_contact-form-name']"));
Name attribute:
WebElement = Firefox.FindElement(By.XPath("//input[@name='name']"));