Step by step selenium – step 4, Finding X-path…1/2

Selenium Basic

In Selenium test automation, if webobject properties does not have id, name, class then we left with 2 options-

Option 1 calculate xpath

Option 2 Calculate XssSelector

In this post, we will calculate about the xpath and various XPath expression for webobjects whose attributed are changing dynamically.

There are 2 types of xpath method

1- Absolute xpath- It is not recomended, as its not optimized and contains full path from a top node which is unique. It increases failure chances as a lot of dependencies are there.

Syntax example- html/body/div[2]/div[4]/div/div/div[1]/div/div/div/div/div[1]/div[1]/div/a

2- Relative xpath- Most preferred, As it is optimized and lesser chances of failure are there, its represented something like-

Relative xpath: //*[@class=’tst-Search’]/td

Xpath representation-


//– Current node

tagname– Every html web object is tagged to tag which is div, image, input, button etc

@– Attribute selection

Attribute – Name of attribute like class, type etc

Value– Value of attibute- given by html designers.

Ther are couple of tools to find xpath-

1- firebug– in firefox browser

2- firepath– in firefox browser

3- chropath– in chrome browser

We will see how to find xpath in chrome browser-

1- Click on top right and then select more tools, in your chrome browser, as shown in below image-

Select on Control + ‘F’ using keyboard keys, you will get highlighted text where xpath can be written and validated. Chrome browser highlights the correct result in User Interface.

Basic Xpath-

Text contains string ‘Value’

Weblink contains values in single quote

Using start with opetor

Using OR as a logical operator when there are 2 values can be of a web object
Xpath=//*[@type’Vale’ OR @name=’val2′]

Using AND as a logical operator when there are 2 values can be of a web object
Xpath=//*[@type’Vale’ AND @name=’val2′]

Text contains value which is inside td

Axes method- Follwoing- Xpath=//*[@type=’Value’]//following::input[1]

Axes method- Ancester – Xpath=//*[text()=’Value’]//ancestor::div


Xpath is one way of finding a element whose attributes valueis chaning or id, class and name is not available.

Xpath is of 2 types- Absolute and Relative.

Xpath usually work fine however in Internet Explorer sometime it didnot work as Internet Explorer has its ownxpath engine which understand bit different than Chrome and Firefox therefore for cross browser automation scritps it is recomended to use CssSelector, My next post is on CssSelector.

Its Prashant Chaudhary, I have 10+ years of experience in software test automation, performance & process automation with various commercial and open source tools. This blog shares my experience on the topic of Automation. The content published on my blog is solely mine, it does not express the views of any of my employers (current or previous).

Leave a Reply

Your email address will not be published. Required fields are marked *

Selenium Basic
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Selenium Basic
Step by step selenium – step 2 -How to enter value in text box and how to click a button.

Abstract- In Previous post, we have learnt how to launch a web-browser in selenium. In this post we will learn how to enter values in a text box and how to click a button. We will be using chrome browser for this posts well. package TestNG_Classes; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; …

Selenium Basic
Step by step selenium – step 1, How to launch a web browser

Introduction – In this blog post, we will see how to run first test in selenium and what are necessary configuration required for invoking browser. There are 2 basic configuration to invoke a browser. First step, download and configure selenium standalone jar. Visit - , Refer below screenshot. Config the …