How can I use the “preceding-column” axis in Xpath for retrieving the data from a row of a table?

220    Asked by CharlesParr in QA Testing , Asked on Jan 15, 2024

 I have been assigned a task that is related to a web-based scraping project that involves the process of extraction of specific data from a particular table on a webpage by using the Xpath. This particular table has rows of data, and I want to retrieve the information from a particular row based on the value in a preciding sibling column. Explain to me how can I use the “ preceding-sibling” axis in Xpath to achieve this particular target. 

Answered by Charles Parr

In the context of selenium, you can use the preceding sibling xpath for the process of retrieving the specific information or data from a row of a particular table.

For example, consider a scenario in which you have an HTML table and you aim to extract data from a specified column based on the value in a preceding column. For this, you can use the axis for navigating the structure.

Here is the example-based scenario given and Xpath expression:-

Let us say you have a table of HTML whose structure would look like this:-


   
       
       
   
   
       
       
   
   
       
       
    ProductPriceApple$1.99Orange$2.49

For extracting the price of a particular product (consider orange), you could use the “preceding sibling” axis to locate the price based on the name of the product:

//td[text()=’Orange’]/following-sibling::td[1]



Your Answer

Interviews

Parent Categories