我正在尝试寻找 <td>
元素使用 Python Selenium
从包含一些特定文本的表格中我可以得到它。但是,当我在所选元素上调用父方法时,它返回表的实例而不是表的父行。我正在做的是
driver = webdriver.Firefox()
driver.get('http://www.example.com')
WebDriverWait(driver, self.explicit_wait).until(
EC.presence_of_element_located((By.ID, "table-id"))
)
td = driver.find_element_by_xpath("//*[contains(text(), 'some texts')]"
parent_row = td.parent # get the parent of td element
但是在 parent_row
驱动程序返回表实例而不是行实例。
帮助将不胜感激
最佳答案
根据 Webdriver specification ,没有办法直接使用WebDriver获取父元素。 .parent
attribute selenium Python 绑定(bind)中的 WebElement
是在内部使用的,并不引用元素的直接父元素。它引用您在以下位置调用 find_element_by_*
的 WebDriver
实例:
parent
Internal reference to the WebDriver instance this element was found from.
您可以使用xpath获取父元素:
parent_row = td.find_element_by_xpath("..")
关于python - 获取包含特定文本的 td,然后找到其 parent 和 sibling ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30439688/