I am trying to find a specific element inside a certain parent element "table_list". However, the webdriver finds all the occurence of my required element on the web page.
def value():
table_list = driver.find_element(By.ID,"table_list")
print(table_list.text)
value_informal = table_list.find_elements(By.XPATH,"//tr[contains(@id,'informal')]")
for i in range(len(value_informal)):
value_td = value_informal[i].find_elements(By.TAG_NAME, "td")
print(value_td[3].text)
I want to find all the "informal" elements in the parent element "table_list" but my code is returning all the occurences of informal on the webpage. (I cannot use by.id,"informal" directly because I am using Partial Text).
I just want to find all the occurences of "informal" inside my specific "table_list".
CodePudding user response:
Change this line of code
value_informal = table_list.find_elements(By.XPATH,"//tr[contains(@id,'informal')]")
to this
value_informal = table_list.find_elements(By.XPATH,".//tr[contains(@id,'informal')]")
.
means intermediate child of the parent element.
CodePudding user response:
As soon as you start your path from /
or //
it ignores search context and starts the path from DOM root. It is much like you would navigate file system in command line.
So in your case you need to start your path with .
which means "current node".