在selenium python中获取子元素

时间:2014-12-22 17:00:04

标签: python selenium selenium-webdriver

大家好我在下面有这个代码,它是一个删除菜单,其中包含进一步的删除实例和删除实例及以下内容。我正在尝试ge子菜单,删除此实例或删除此实例和以下,但到目前为止没有运气。

<li class="context-menu-item context-menu-submenu icon icon-delete">
    <span>Delete</span>
    <ul class="context-menu-list " style="width: 213px; z-index: 10; top: -21px; left:245px;">
        <li class="context-menu-item icon icon-delete">
            <span>Delete This Instance</span>
        </li>
        <li class="context-menu-item icon icon-delete">
           <span>Delete This Instance and All Following</span>
       </li>
    </ul>

这是我到目前为止所拥有的

driver.find_element_by_class_name("icon-delete").click()

....这样可以找到主要删除菜单并打开它

driver.find_element_by_xpath( "//ul[@class='context-menu-list']/..//li[@class='icon delete' and text()='Delete This Instance']" ).click()

...这是我收到错误的地方。

2 个答案:

答案 0 :(得分:1)

使用 Firepath 进行检查时,您的xpath "//ul[@class='context-menu-list']/..//li[@class='icon delete' and text()='Delete This Instance']"没有产生任何元素,因此产生了异常。

您可以使用以下xpath来检索所需的元素:

1-找到&#39;删除此实例&#39;

//li[contains(@class,'icon-delete')]//span[.='Delete This Instance']

2-找到&#39;删除此实例和所有关注&#39;

//li[contains(@class,'icon-delete')]//span[.='Delete This Instance and All Following']

答案 1 :(得分:0)

这应该适合你:

driver.find_element_by_xpath("//li[@class='icon-delete']/span[text()='Delete This Instance']").click()