Reputation: 77
I want to select a value from a drop down menu that has several menus inside it
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("../../../../resources/images/menu_up.gif"); 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("../../../../resources/images/menu_sub.gif")" arsubmenu="0"> </td>
</tr>
</tbody>
</table>
</div>
<div class="MenuScrollDown" style="background-image: url("../../../../resources/images/menu_down.gif"); 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("../../../../resources/images/menu_sub.gif")" arsubmenu="4"> </td>
</tr>
<tr class="MenuTableRow">
<td class="MenuEntryName" nowrap="">IBM Outsourcing</td>
<td class="MenuEntrySub" style="background-image:url("../../../../resources/images/menu_sub.gif")" arsubmenu="5"> </td>
</tr>
</tbody>
</table>
</div>
Upvotes: 1
Views: 152
Reputation: 29362
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()
Upvotes: 1