如何遍历回Selenium中元素的父元素Python

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

我有这个元素

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

即在html和select元素中搜索文本 假设元素有 xpath

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

有没有办法在这个元素中 return div 例如:

element cd.. cd..

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

以及我如何从元素中获取完整的 xpath

就像在MS DOS you traverse back to the parent directory using cd.., using 中的方法一样,您可以移动到父元素,如下所示:

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')]/../..")