如何使用 python selenium 从此下拉列表中进行选择?

问题描述 投票:0回答:1

我正在尝试从下拉列表中进行选择,该列表大部分时间都有效,但有时它会选择错误的列表项。

我目前的代码是:

# select popup window
popup = WebDriverWait(driver, timeout=15).until(lambda d: d.find_element(By.ID, 'form1'))

#click arrow to open drop down
popup.find_element(By.ID, 'ctl00_ctl00_siteContent_siteContent_SectionType2_GridEditor_ListEditor_EditorListView_ctrl1_FieldInputLoadOnDemandCombo_Arrow').click()

# Select item from list
WebDriverWait(driver, timeout=15).until(lambda d: d.find_element(By.XPATH,'//*[@id="ctl00_ctl00_siteContent_siteContent_SectionType2_GridEditor_ListEditor_EditorListView_ctrl1_FieldInputLoadOnDemandCombo_DropDown"]/div[2]/ul/li[28]')).click()

弹出窗口打开,我选择然后单击箭头打开下拉列表,然后从中选择我想要的项目。

html 看起来像:

<body>
  <form method="post" action="./Search" id="form1">
    <div class="rcbSlide" style="z-index: 6000; display: block; top: 383px; left: 1264.31px; overflow: visible; height: 596px;">
      <div id="ctl00_ctl00_siteContent_siteContent_SectionType2_GridEditor_ListEditor_EditorListView_ctrl1_FieldInputLoadOnDemandCombo_DropDown" class="RadComboBoxDropDown RadComboBoxDropDown_Connect cup-combo-dropdown rcbAutoWidth rcbAutoWidthResizer" style="display: block; top: 0px; visibility: visible; transition: none 0s ease 0s; height: 594px;">
        <div id="ctl00_ctl00_siteContent_siteContent_SectionType2_GridEditor_ListEditor_EditorListView_ctrl1_FieldInputLoadOnDemandCombo_Header" class="rcbHeader">
          <span class="item-header">
            <span class="item-column item-code-column">Code</span>
            <span class="item-column">Description</span>
          </span>
        </div>
        <div class="rcbScroll rcbWidth" style="height: 564px;">
          <ul class="rcbList">
            <li class="rcbItem  rcbTemplate">
            <li class="rcbItem  rcbTemplate">
            <li class="rcbItem  rcbTemplate">
              <span class="item-row">
                <span id="ctl00_ctl00_siteContent_siteContent_SectionType2_GridEditor_ListEditor_EditorListView_ctrl1_FieldInputLoadOnDemandCombo_i27_lblElementValue0" class="item-column item-code-column" colindex="0">AUBANK</span>
                <span id="ctl00_ctl00_siteContent_siteContent_SectionType2_GridEditor_ListEditor_EditorListView_ctrl1_FieldInputLoadOnDemandCombo_i27_lblElementValue1" class="item-column" colindex="1">AUD BANKING TYPE</span>
              </span>

这是 for 循环的一部分,所以它会执行很多次,有时会起作用,但有时却不起作用

python selenium-webdriver
1个回答
0
投票

要使用 Python Selenium 从下拉列表中选择一个项目,您可以使用 selenium.webdriver.support.ui 模块中的 Select 类。这是一个适用于您的案例的示例代码片段:

from selenium.webdriver.support.ui import Select

# select popup window
popup = WebDriverWait(driver, timeout=15).until(lambda d: d.find_element(By.ID, 'form1'))

# click arrow to open drop down
popup.find_element(By.ID, 'ctl00_ctl00_siteContent_siteContent_SectionType2_GridEditor_ListEditor_EditorListView_ctrl1_FieldInputLoadOnDemandCombo_Arrow').click()

# wait for the dropdown list to appear
dropdown_list = WebDriverWait(driver, timeout=15).until(EC.presence_of_element_located((By.ID, "ctl00_ctl00_siteContent_siteContent_SectionType2_GridEditor_ListEditor_EditorListView_ctrl1_FieldInputLoadOnDemandCombo_DropDown")))

# select an item from the dropdown list
dropdown = Select(dropdown_list)
dropdown.select_by_visible_text("AUD BANKING TYPE")

这段代码首先使用find_element方法找到弹窗,点击下拉箭头打开列表。然后它使用 presence_of_element_located 方法等待下拉列表出现并创建一个

从下拉列表中选择对象。最后,它使用 select_by_visible_text 方法从下拉列表中选择所需的项目。您可以调整 select_by_visible_text 方法的输入以从下拉列表中选择所需的项目。

© www.soinside.com 2019 - 2024. All rights reserved.