Nro
Nro

Reputation: 349

Getting a child element in selenium python

Hi guys I have this code below, its a delete menu, which contains a further delete instance and delete instance and following. I am trying to ge to the sub menus, delete this instance or delete this instance and following, but so far no luck.

<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>

This is what I have so far

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

....this works it finds the main delete menu and opens it

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

...this is where I get the error.

Upvotes: 0

Views: 2142

Answers (2)

Subh
Subh

Reputation: 4424

When checked using Firepath, your xpath "//ul[@class='context-menu-list']/..//li[@class='icon delete' and text()='Delete This Instance']" didn't result in any element, hence the resulting exception.

You can use the below xpaths for retrieving the required elements:

1- To locate the 'Delete This Instance' :

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

2- To locate the 'Delete This Instance and All Following' :

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

Upvotes: 1

Surya
Surya

Reputation: 4536

This should work for you:

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

Upvotes: 0

Related Questions