Home > database >  Choose XPATH based on <th> string value with selenium
Choose XPATH based on <th> string value with selenium

Time:11-11

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")

CodePudding user response:

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
  • Related