While automating modern Web pages with a lot of dynamic content, it becomes imperative to learn the usage of Advanced XPath expressions. Since teaching you the usage of XPaths from the very beginning is out of scope of this article, you can get acquainted with XPath basics using the following article from W3Schools - XPath Tutorial.
Let us take 3 example scenarios where we need to find the XPaths for the dynamic date elements:
1. Fetch Today's date in the date widget.
2. Fetch Tomorrow's date in the date widget.
3. Fetch 'Today + n days' date in the date widget.
Advanced Example 1: Fetch today's date
In most cases, there will be a distinct class name for today's date html element. This will be different for different date libraries but there exists a pattern. We just need to find this pattern to get the XPath for today.
In this case, you can see that the class name consists of "highlight". So we can write XPath for today as:
Advanced Example 2: Fetch 'tomorrow's date
Based on Example 1, we have XPath for today.
For the next day i.e tomorrow, it would be the following 'td' element as shown below:
Advanced Example 3: Fetch 'today + 7 days' date
Based on the above Examples, we have XPath for today.
For the 7th day i.e 7 days from today, it would be the following 'td' element with postion=7 as shown below: