Selenium: All about XPath Locator in WebDriver

As we have started writing simple web application login like scripts, its time to move one step ahead. Till now we were finding web elements using general locators like id, name, classname etc. but sometimes it is difficult to find elements using these locators. What if elements don't have these locators, What if some elements are dynamically changing, to answer all these questions, we have Xpath.

Xpath is used to find complex and dynamically changing web elements of the web page. It is defined as XML path, it is basically a syntax for finding web element using its XML path expression. It takes advantage of HTML DOM structure while finding web elements. 

Syntax:

Xpath= //tagname[@attribute='value']


  • // : current node
  • tagname : name of the tags like input, a, img, label etc.
  • attribute : name of the attributes like id, name, classname etc.
  • value : value of that particular attribute
There are mainly two types of Xpath.
  1. Absolute Xpath: Xpath finds element by its root node which is the first node of the web page but it is almost obsolete now as it is difficult to find dynamically changing elements and we need to write long and complex Xpath which leads to errors.
  2. Relative Xpath: Xpath finds element by current node. Hence, we need not write long and complex Xpath and it is easy to locate elements. We will use relative Xpath in all our explanation.
Example:

[source: www.facebook.com ]




Consider above Facebook scenario, suppose we want to automate sign up process so we can make multiple accounts in very less time. (for legal use only !) and we want to find first name using Xpath, our Xpath expression will be:

Xpath: //input(@name='firstname')

In script, we can write as :

driver.findElement(By.xpath("//input(@name='firstname')"));

Xpath Axes:

There are some Xpath axes to find dynamically changing web elements. Some methods and keywords can be used for that.

  1. AND & OR: We can use Xpath combinations using these two logical conditions to find web elements.
  2. Contains: We can use contains keyword to find dynamic elements as here we need not specify full value of attribute. As per above example, we can write Xpath as Xpath: //input[contains(@name, 'name')], we need not specify complete attribute value as 'firstname'. This will be really helpful if dynamically it changes value to firstname, lastname, middlename etc.
  3. Start with: We can find element using this keyword by providing only starting letters of the attribute value. As per above example, we can write Xpath as Xpath: //input[starts-with(@name, 'first')], we need not specify complete attribute value as 'firstname'.
We can conclude that Xpath is very powerful option to find complex and dynamically changing web elements of the web page. Try to write Xpath for different web elements for different web applications and say good bye to 'Element not found ' exception !!

Cheers !



Comments

More posts on selenium...

Selenium/Java-OOPs-Encapsulation

Selenium/Java-OOPs-Abstraction

Selenium: Automate Web Application Login

Selenium/Java- CoC

Selenium/Java-OOPs-Inheritance

Selenium: Waits

Selenium: Capturing the screenshot

Selenium: BDD- The Disruptive Framework

Selenium: Frameworks