top of page
Writer's picturemanali patil

Strategies for Handling Dynamic Web Tables

Web tables are used to showcase data like seen on digital spreadsheets/data sheets. They have information in an arranged manner on web applications. Web tables can be of two types: Static and Dynamic. Static web tables are arranged in a predetermined number of rows and columns, basically they have fixed structure. Whereas, as the name suggests dynamic tables keep on constantly changing. Therefore, we need powerful methods to handle them. The following blog will help you understand the various strategies we can use to extract data effectively from a dynamic web table.


Understanding Dynamic Xpath :

The XML Path Language (XPath) is used to uniquely identify or address parts of an XML document. It is basically used to traverse through the elements and attributes that are present in XML. Xpaths helps to uniquely identify the required element to automate without giving any duplicates. Xpath is like a map that helps Selenium users to find a way through mesh of HTML elements on a web page of an application. Dynamic Xpath can be used in order to locate the required element accurately by decreasing the number of matching results. Xpath must be written in a such a manner that it should be able to handle the changing layouts and attributes of dynamic web tables.


Tactics for Handling Dynamic XPath:


1. Relative XPath:

Let’s consider that you are trying to search for an address in a town. While trying to achieve that you need to rely on famous landmarks as our reference point, similarly Xpath tries to navigate to its stable parent elements in the HTML hierarchy as its reference point. If the dynamic element is located relative to a stable element, you can leverage the relationship between these two elements to locate the dynamic element. By establishing this, Xpath has the ability to be flexible enough to handle the changes in the webtable’s layout, similar to what you do while navigating through a town using well-known landmarks as our guide.

Example:



2. Dynamic Elements:

While travelling across a town you need to adapt to the changing traffic lights and the road conditions. Similarly, you can use regular expressions, varied patterns, partial attributes to locate the desired element. Selenium functions like contains(), ends-with(), starts-with() are useful to help identify the dynamic elements. This is like we travel through the town traffic we also adjust to the changing traffic signals to reach smoothly and safely.

Example:



3. Locating by index positions:

Imagine that you are in a library, and you need to find a book written by an author. In that case you ask the details to the librarian, and she points out the correct shelf and drawers. Like that you can use index positions in xpath to provide accurate position within the web tables, enabling us testers to identify the specific web element despite the changes in those tables. This method is alike to navigating through the library shelves blocks, where precise location helps navigate through all the stack of books.

Example:



4. Iterating Through Rows and Columns:

Now visualize that the librarian is not present to guide us to the book you are looking for, so you go on looking for it through each shelf. Similarly, you can iterate through the rows and columns of the whole web table to find the exact web element from the maze of HTML. In this approach we usually first get the number of rows and columns in the table using XPath. Then, we iterate through each row and column using nested for loops.

Example:




5. Using Multiple Attributes:

If you are trying to search for an address what is the first thing you ask a person for apart from the given address, yes, you ask for a famous landmark. But sometimes even that landmark doesn’t help to locate the address, so you request another secondary landmark. And combining both directs you to the correct location. Likewise, searching for an element with a single attribute proves insufficient so it’s a good idea to combine it with another attribute in creating an Xpath for identifying the dynamic element. To achieve this, you can create a locator that matches the dynamic element based on multiple attribute values by using logical operators like and or in your XPath or CSS selector. Making use of such combination of attributes the specific element can be uniquely identified.

Example:




6. Using dynamic waits:

As soon as we start to engage with a Web application, it starts to load new content. Consequently, web tables also become dynamic and start loading additional information as we keep on exploring the pages. Selenium has explicit wait strategies to handle this dynamic change in content. Explicit waits ensure that a specific condition is met before executing the next steps. For example, it checks if the elements are fully loaded before interacting with them.

You can use methods like WebDriverWait and expected conditions like visibilityOfElementLocated or elementToBeClickable to wait until the dynamic element becomes visible or interactive before locating it.

Example:



7. Pagination:

Visualize, we are testing a library website which has a variety of books and tons of data. All of this can’t be showcased in on go on a single page, so the content is distributed over several pages. Pagination allows users to navigate through data in smaller, manageable chunks, enhancing user experience and performance. To access the correct data in a web table we need to traverse through all the pages in it. Commonly used options to achieve this are by having options like “Next, “Previous” or numbered page links.

Example:




Pro Tips

· Use relative XPath to make your locators resilient to changes.

· Implement explicit waits to handle asynchronous table updates.

· Always clean up your WebDriver sessions to prevent resource leaks.

· It is important to implement the appropriate error handling to handle scenarios such as :

- Table doesn’t exist on the web page.

- No rows in the table.

- Enough number of rows are not available to present the Next page.

59 views
bottom of page