Delivered by FeedBurner

How To Locate Element By XPATH In Selenium 2/WebDriver With Example

We have learnt most of all the way of locating element in webdriver software testing tool like Locating Element By ID, Locating Element By Name, Locating Element By Class Name, Locating Element By Tag Name, Locating Element By Link Text Or Partial Link and Locating Element By CSS Selector. One another method of locating element in selenium webdriver software testing tool is By XPATH of element. It is most popular and best way to locate element in WebDriver software testing tool . However you can use other ways too to locate element from page of software web application.

You can write Xpath of any element by many different ways as described in This Post.

Above given Image shows the firebug view of First name text box. You can view THIS POST to know how to install firebug and firepath. Now let me give you few examples of how to write syntax to locate it in webdriver software testing tool using xPath in 2 different ways.

1. driver.findElement(By.xpath("//input[@name='fname']"));
2. driver.findElement(By.xpath("//input[contains(@name,'fname')]"));

I have used xPath to locate element in above both the syntax. We can use anyone from above to locate that specific element. Let we use it in practical example as bellow.

Copy bellow given @Test method part and replace it with the @Test method part of example given on this page.
(Note : @Test method is marked with pink color in that example).

 public void test() 
   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
   driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");//Locate element by cssSelector and then type the text in it.


  1. Hi. Can you post the different ways of finding xpath.

    1. You will find different ways to locate xpath on ->

  2. Please post different ways to find xpath

    1. Hi,

      Right click on element, select Inspect by Xpath, u will get code in Xpath "html/body/div[1]/div[1]/div[2]/div[2]/div/div/ul/li[1]/div/div[2]/div[1]/ul[1]...." in this format. Same process for locating any kind of elements. Make sure that u should cross verify element is highlighting or not.

      This is the best way to locate any elements.

      Example: driver.findElement(By.xpath("html/body/div[1]/div[1]/div[2]/div[2]/div/div/ul/li[1]/a/span")).click();

    2. Hi,

      Their is one more method to Locating element with Xpath. Right click on element and click on Inspect by FirePath. You will xpath code in this format. It is most usefull and easiest method to locate any element.
      i.e. html/body/div[1]/div[1]/div[2]/div[2]/div/div/ul/li[1]/a/span

      Use the same code while writing the script.

      Example: driver.findElement(By.xpath("html/body/div[1]/div[1]/div[2]/div[2]/div/div/ul/li[1]/a/span")).click();

    3. This becomes too long and is not recommended approach.

  3. Better not to use absolute xpath as it will change dynamically.Go for relative xpath like ("//input[@name='fname']")

  4. Better not to use absolute xpath ("html/body/div[1]/div[1]/div[2]/div[2]/div/div/ul/li[1]/a/span"),as it will change dynamically.go with relative xpath("//input[@name='fname']").