从Selenium Python中的下拉列表中选择选项值

发布于 2025-01-23 21:14:55 字数 2545 浏览 0 评论 0原文

我有以下HTML元素:

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

我在从下拉列表中选择“ 4”选项时遇到困难。

我尝试了以下代码,

(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')

但它们似乎都没有用。请帮忙。

PS每次选项的XPATH中的ID都会改变

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文