-1

https://demo.guru99.com/test/web-table-element.php

How to find XPath? Dynamically how to get XPath for changing values, get current price for company that starts with letter A in the above mentioned url. Table changes dynamically.

//a[contains(text(),"Apollo Hospitals")]
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • The essence of screen-scraping is that you have to guess what parts of the page are going to remain constant, and what parts are going to change. If everything changes, you're hosed. Your task is generally to find the variable content by exploiting its relationship to fixed content. There's no correct way of doing it; it's guesswork, and screen-scraping will always break if the site decides to make a substantial change. – Michael Kay Jun 29 '23 at 16:52

2 Answers2

1

Below XPath expression is what you need:

//table[@class='dataTable']//td[1]//text()[starts-with(normalize-space(), 'A')]//following::td[3]

Explanation: This XPath expression locates the Current Price of the Company which starts with letter A.

Let me try to explain part by part:

  • Part 1. //table[@class='dataTable'] - Locate the table element with attribute=class and value=dataTable

  • Part 2. //td[1] - locates first column of the table

  • Part 3. //text()[starts-with(normalize-space(), 'A')] - locates text which starts with letter A within the current node

  • Part 4. //following::td[3] -- locates 3rd column from the current node which is Current Prince (Rs)

For your reference(see below):

enter image description here

Shawn
  • 4,064
  • 2
  • 11
  • 23
0

To extract the current price for company that starts with letter A you can use either of the following locator strategies:

  • XPATH and starts-with:

    //table[@class='dataTable']//tbody//tr/td[./a[starts-with(normalize-space(), 'A')]]//following::td[3]
    
  • XPATH and following:

    //table[@class='dataTable']//tbody//tr/td[./a[contains(., 'A')]]//following::td[3]
    
  • XPATH and following-sibling:

    //table[@class='dataTable']//tbody//tr/td[./a[contains(., 'A')]]//following-sibling::td[3]
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352