Selenium / xpath-找不到元素,找到父div

时间:2018-08-10 13:00:31

标签: python selenium xpath

  

使用Python 3+和Selenium> IE驱动程序

我正在运行一个python脚本来自动填写页面上的表单。之所以找到父元素,是因为存在多个具有相同类的元素,除了基于文本的标题外没有其他标识符。

HTML:

<div class="pg_BoxContents"> /* Div I am trying to select */
<div class="Title">Replay Permissions:</div>
</div>

Python /硒:

replay_form = browser.find_element_by_xpath("//span[contains(text(),'Replay Permissions:')]/ancestor::div[contains(@class, 'pg_BoxContents')]")

错误:

selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with xpath == //span[contains(text(),'Details:')]/ancestor::div[contains(@class, 'pg_BoxContents')]

1 个答案:

答案 0 :(得分:0)

替换:

//span[contains(text(),'Replay Permissions:')]/ancestor::div[contains(@class, 'pg_BoxContents')]

具有:

//div[contains(text(),'Replay Permissions:')]/ancestor::div[contains(@class, 'pg_BoxContents')]

您使用的//span[...所提供的HTML中没有span。但是有一个div