Choosing an option value from dropdown list in Selenium Python

Viewed 23

I have this following HTML element:

<div class="inlineCenter GuestNumDropdown">
  <select name="participants" id="ui-id-1" style="display: none;">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select><span tabindex="0" id="ui-id-1-button" role="combobox" aria-expanded="true" aria-autocomplete="list" aria-owns="ui-id-1-menu" aria-haspopup="true" class="ui-selectmenu-button inputElement required ui-button ui-widget ui-selectmenu-button-open ui-corner-top"
    aria-activedescendant="ui-id-24" aria-labelledby="ui-id-21" aria-disabled="false"><span class="ui-selectmenu-icon ui-icon ui-icon-triangle-1-s"></span><span class="ui-selectmenu-text">1</span></span>
  <div class="ui-selectmenu-menu ui-front ui-selectmenu-open"
    style="top: 980px; left: 88.5px;">
    <ul aria-hidden="false" aria-labelledby="ui-id-1-button" id="ui-id-1-menu" role="listbox" tabindex="0" class="ui-menu ui-corner-bottom ui-widget ui-widget-content overflow" aria-activedescendant="ui-id-24" aria-disabled="false" style="width: 60px;">
      <li class="ui-menu-item">
        <div id="ui-id-21" tabindex="-1" role="option" class="ui-menu-item-wrapper">1</div>
      </li>
      <li class="ui-menu-item">
        <div id="ui-id-22" tabindex="-1" role="option" class="ui-menu-item-wrapper">2</div>
      </li>
      <li class="ui-menu-item">
        <div id="ui-id-23" tabindex="-1" role="option" class="ui-menu-item-wrapper">3</div>
      </li>
      <li class="ui-menu-item">
        <div id="ui-id-24" tabindex="-1" role="option" class="ui-menu-item-wrapper ui-state-active">4</div>
      </li>
    </ul>
  </div>
</div>

I'm having trouble selecting the option with the value '4' from the dropdown list.

I have tried the following codes,

(1)

driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@class="ui-menu-item-wrapper ui-state-active" and text()="4"]'))))

(2)

pax = driver.find_element_by_id("ui-id-1")
pax.send_keys("4")

(3)

select= Select(driver.find_element_by_id("ui-id-1"))
select.select_by_value('4')

But none of them seems to work. Please help.

P.S. The id in the XPaths of the options changes every time

0 Answers
Related