'How to traverse back to the parent of an element in Selenium Python

i have this element

element = driver.find_element_by_xpath("//*[contains(text(), '/cargas')]")

that search text in html and select one element suppose the element has the xpath

//*[@id="main"]/div[3]/div/div[2]/div[2]/div[11]

is there a way to return div's in this element eg:

element cd.. cd..

//*[@id="main"]/div[3]/div/div[2]

and how i get the full xpath from element



Solution 1:[1]

Just like the way in MS DOS you traverse back to the parent directory using cd.., using xpath you can move to the parent element as follows:

element = driver.find_element_by_xpath("//*[contains(text(), '/cargas')]")
parent_element = driver.find_element_by_xpath("//*[contains(text(), '/cargas')]/..")
grand_parent_element = driver.find_element_by_xpath("//*[contains(text(), '/cargas')]/../..")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium