Selenium-7 || Let’s learn to create complex XPath.

There are few scenarios where we get stuck and unable to locate web element on the page. In this blog I will discuss such scenarios.

Here I have taken example of a simple Calendar widget and will learn how to get few type of dates as mentioned below:

  1. Print Current date.
  2. Use following-sibling to get consecutive dates after current date
  3. Use indexing to get random dates
  4. Traversing through various dates using findElements() and foreach() loop.
  5. Use double dots(..) to extract first date in current week.
  6. Use preceding-sibling to get consecutive dates before current dat

Sample URL

Launch the above URL in chrome browser and Scroll down the page to navigate the calendar widget in the right-hand side as mentioned in the below image. Please close pop-up box if present any.


Use following-sibling:: key in XPath

Consider a scenario where we need to locate current date in a calendar widget as mentioned below:

Calendar Example

This is very simple XPath and we could extract with simple method:

// Locate only current Date in the Calendar Widget.
String dateXPath="//td[@id='today']";
WebElement calenderElement=driver.findElement(By.xpath(dateXPath));
String currentDate=calenderElement.getText();
System.out.println("Current Date: "+currentDate);

But, suppose we need to get next consecutive date after current date in current week then it is impossible to do with normal method.

Consecutive dates after current Date

So we have to use following-sibling:: method like  //td[@id=’today’]/following-sibling::td

This method uses following-sibling key with :: key, which means after current node webdriver will return next elements of given HTML tag type.In the first image we can see that after current date with <td id=”today”> there are multiple <td> tags are present so we have to use ‘td’ after :: operator to get next elements in the same table row.

// get next consecutive date in current week using following-sibling:: and findElement() methods
		String childXpath="//td[@id='today']/following-sibling::td";
		WebElement dateElement=driver.findElement(By.xpath(childXpath));
		String nextDate=dateElement.getText();
		System.out.println("Next Date :"+nextDate);

Indexing of elements while using XPath

As we saw that using the above XPath we could get only next date in the same week. But what if we need to access second or third element after the current date or current element.

Actually the same XPath //td[@id=’today’]/following-sibling::td contains all the successive elements. But to access specific position we have to use indexing. It’s very simple process. We just need to add [index_value] at the end of the the XPath like:

Index_value should be an integer value starting from 0 position.

// get any random consecutive date in current week using index number
		String childXpath1="//td[@id='today']/following-sibling::td[2]";
		WebElement dateElement1=driver.findElement(By.xpath(childXpath1));
		String randomDate=dateElement1.getText();
		System.out.println("Random Date :"+randomDate);
Selenium Tutorials:

Window Handling: 

Test Frameworks: 

Related Links: 

Traversing through all the elements using findElements() method

We could also extract all the elements at once using findElements() method of WebDriver instance.

// get all the consecutive dates after current date in current week using following-sibling:: and fineElements() methods
String childXpath="//td[@id='today']/following-sibling::td";
		List<WebElement> dateElemnts=driver.findElements(By.xpath(childXpath));
		System.out.println("\nAll the consecutive dates after current date:");
		for (WebElement webElement : dateElemnts) {
  • List<WebElement>: List object to store all the web element instances.
  • findElement(): Used to access all the consecutive dates after current date.
  • Foreach loop: To traverse through all the dates after current date.

Using double dots(..) in XPath

Double dots after any valid XPath will take the compiler to one step above in the HTML hierarchy. Or we can see navigates to Parent node of the current node.

For example,

  • XPath //td[@id=’today’] currently pointing to current date tag.
  • Now, //td[@id=’today’]/.. Would point to its parent HTML tag.

Suppose, a scenario where we have to extract date of first day in current week.

In this case we have to follow below steps to get correct XPath:

  1. Locate current date using XPath //td[@id=’today’]
  2. Locate parent of this row using XPath //td[@id=’today’]/..
  3. Locate first date of current week using XPath //td[@id=’today’]/../td

So final XPath would be //td[@id=’today’]/../td

// To get 'first date in current week' using double dots(..) in XPath
		String childXpath2="//td[@id='today']/../td";
		WebElement dateElement2=driver.findElement(By.xpath(childXpath2));
		String firstDate=dateElement2.getText();
		System.out.println("\nFirst Date in Current Week :"+firstDate);

Note: The same XPath can be used to get all the child nodes of current HTML Parent Tag. Means all the dates of current week can be extracted. We can traverse using findElements() and foreach() loop as we discussed in previous example.

Use preceding-sibling:: in XPath

As we read that to access all the consecutive nodes after the current node, we have to use following-sibling:: key. The same way if we have to access all the consecutive nodes prior to the current node we have to use preceding-siblings:: key.

Hence, target XPath would be //td[@id=’today’]/preceding-sibling::td

We can traverse through each node using findElements() and foreach() loop as we discussed in previous example.

// get all the consecutive dates prior to current date in current week using preceding-sibling:: and fineElements() methods
		String childXpath3="//td[@id='today']/preceding-sibling::td";
		List<WebElement> element=driver.findElements(By.xpath(childXpath3));
		System.out.println("\nAll the consecutive dates prior to current date:");
		for (WebElement webElement : element) {
XPath is the best way to locate web elements.

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 2
  • Page views today : 2
  • Total visitors : 51,167
  • Total page view: 70,434

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.