0

There is a table that I want to get the XPATH of, however the amount of rows and columns is inconsistent across results so I can't just right click and get copy the full XPATH.

My current code:

result_priority_number = driver.find_element(By.XPATH, "/html/body/div/div[2]/div[6]/div/div[2]/table/tbody/tr[18]/td[2]")

The table header names though are always consistent. How do I get the value of an element where the table header specifically says something (i.e. "Priority Number")

dennis
  • 19
  • 5

1 Answers1

1

I can't just right click and get copy the full XPATH.

Never use this method. Xpath has a very useful feature for search! It isn't just for nested pathing!


//td[contains(text(),'header value')]

or if it has many tables and you want only one of its:


//table[@id='id_of_table']//td[contains(text(),'header value')]

or the table hasn't id or class:


//table[2]//td[contains(text(),'header value')]

where 2 is index of table in page

and other many feature for searching in html nodes

in your case, for get Filing language:

//td[contains(text(),'Filing language')]/following-sibling::td
PersianMan
  • 924
  • 1
  • 12
  • 29
  • Thank you, it's my first time encountering python and XPATH as well, watched 1 video and freestyled the rest. Submitting as answer – dennis Nov 10 '21 at 20:24
  • 1
    @dennis , read more: https://stackoverflow.com/a/56971704/4650634 and https://stackoverflow.com/a/2994336/4650634 – PersianMan Nov 10 '21 at 20:28
  • 1
    @dennis in your case `//td[contains(text(),'Priority number')]/following-sibling::td` and `//td[contains(text(),'Priority number')]/parent::tr/following-sibling::tr[1]//td` and `//td[contains(text(),'Priority number')]/parent::tr/following-sibling::tr[2]//td` – PersianMan Nov 10 '21 at 20:32
  • 1
    @dennis You can test xpath with google chrome inspect element, open it and press `ctrl+f` and paste xpath query in search box – PersianMan Nov 10 '21 at 20:34