1

I want to select a value from a drop down menu that has several menus inside it enter image description here

I use the following code:

# CLICK ARROW
driver.find_element(By.CSS_SELECTOR, '#WIN_7_1000000217 > a:nth-child(3)').click()
# 1ST MENU
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.MenuOuter:nth-child(7) > div:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6)')))
menu_1 = driver.find_element(By.CSS_SELECTOR, 'div.MenuOuter:nth-child(7) > div:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6)')
actions.move_to_element(menu_1).click().perform()
# 2ND MENU
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.MenuOuter:nth-child(11) > div:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1)')))
menu_2 = driver.find_element(By.CSS_SELECTOR, 'div.MenuOuter:nth-child(9) > div:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1)')
actions.move_to_element(menu_2).click().perform()
# 3RD MENU
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'tr.MenuTableRow:nth-child(35)')))
menu_3 = driver.find_element(By.CSS_SELECTOR, 'tr.MenuTableRow:nth-child(35)')
actions.move_to_element(menu_3).click().perform()

However I am not able to select the second menu, its CSS_SELECTOR is:

div.MenuOuter:nth-child(9) > div:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1)

Sometimes it works this way but the problem is that CSS_SELECTOR changes every time the number of the first nth-child, sometimes it's 9, 11 or 12... Is there a way to solve this and locate the element I want?

Html code for the second menu visible in the image:

<div class="MenuScrollUp" style="background-image: url(&quot;../../../../resources/images/menu_up.gif&quot;); width: 62px; visibility: hidden;"></div>
<div class="MenuTableContainer">
   <table class="MenuTable" style="width: 62px;" cellspacing="0" cellpadding="0">
      <tbody class="MenuTableBody">
         <tr class="MenuTableRow">
            <td class="MenuEntryNameHover" nowrap="">IT Support</td>
            <td class="MenuEntrySubHover" style="background-image:url(&quot;../../../../resources/images/menu_sub.gif&quot;)" arsubmenu="0">&nbsp;</td>
         </tr>
      </tbody>
   </table>
</div>
<div class="MenuScrollDown" style="background-image: url(&quot;../../../../resources/images/menu_down.gif&quot;); width: 62px; visibility: hidden;"></div>

Html code for ESI - Espirito Santo Informatica and IBM Outsourcing:

<div class="MenuTableContainer">
    <table class="MenuTable" style="width: 174px;" cellspacing="0" cellpadding="0">
        <tbody class="MenuTableBody">
            <tr class="MenuTableRow">
                <td class="MenuEntryName" nowrap="">ESI - Espirito Santo Informatica</td>
                <td class="MenuEntrySub" style="background-image:url(&quot;../../../../resources/images/menu_sub.gif&quot;)" arsubmenu="4">&nbsp;</td>
            </tr>
            <tr class="MenuTableRow">
                <td class="MenuEntryName" nowrap="">IBM Outsourcing</td>
                <td class="MenuEntrySub" style="background-image:url(&quot;../../../../resources/images/menu_sub.gif&quot;)" arsubmenu="5">&nbsp;</td>
            </tr>
        </tbody>
    </table>
</div>
5
  • 1
    Is URL public ? Commented Jun 15, 2021 at 9:20
  • Nope, sorry :(( Commented Jun 15, 2021 at 9:20
  • 2
    Need to see HTML to give you proper solution Commented Jun 15, 2021 at 9:21
  • 1
    Can you do it with text shown in the menu ? Like Select IBM Outsourcing based on text... something like //div[contains(text(), 'IBM Outsourcing')] - this is just a typical xpath Commented Jun 15, 2021 at 9:24
  • I added the html code from the second menu visible in the image Commented Jun 15, 2021 at 9:27

1 Answer 1

1

Why not xpath ?

//td[contains(text() , 'IT Support')]

something like this :

# 2ND MENU
actions.move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[contains(text() , 'IT Support')]")))).click().perform()
Sign up to request clarification or add additional context in comments.

31 Comments

What is the error when it did not work ? and Are you doing the same with menu 1 and 3 ?
How are you selecting the Menu 1 ? after click on arrow
You need to change this line div.MenuOuter:nth-child(7) > div:nth-child(2) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) write xpath or a reliable css
Can you share HTML for IBM Outsourcing also ?
let me have a look, I will get back to you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.