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

www.allinoneblogs.com/

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.

Calendar_widget

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:
//td[@id=’today’]

// 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:

//td[@id=’today’]/following-sibling::td[2]
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) {
			System.out.println(webElement.getText());
		}
  • 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) {
			System.out.println(webElement.getText());
		}
XPath is the best way to locate web elements.

1257total visits,3visits today

Leave a Reply

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