带有硒的 select2 下拉列表

select2 dropdown with selenium

我在 WordPress 产品页面中有一个下拉菜单,它启用了 select2 ajax。

我已经设法使用 selenium 在下拉列表中显示选项。

但我无法select列表中的选项之一。

我可以使用以下代码获取元素。但是该元素不可转换为 Select 也不可点击。

driver.findElement(By.cssSelector("[class='multiselect attribute_values wc-enhanced-select select2-hidden-accessible enhanced']"))

任何想法如何select它的任何选项?

下拉菜单的 HTML 代码是

<tr>
  <td class="attribute_name">
    <label>Name:</label>
    <strong>Brand</strong>
    <input type="hidden" name="attribute_names[0]" value="pa_brand">
    <input type="hidden" name="attribute_position[0]" class="attribute_position" value="0">
  </td>
  <td rowspan="3">
    <label>Value(s):</label>
    <select multiple="" data-placeholder="Select terms" class="multiselect attribute_values wc-enhanced-select select2-hidden-accessible enhanced" name="attribute_values[0][]" tabindex="-1" aria-hidden="true">
      <option value="107">Adidas</option>
      <option value="110">Gul Ahmed</option>
      <option value="111">Khadi</option>
    </select>
    <span class="select2 select2-container select2-container--default select2-container--above select2-container--open" dir="ltr" style="width: auto;">
         <span class="selection">
            <span class="select2-selection select2-selection--multiple" aria-haspopup="true" aria-expanded="true" tabindex="-1" aria-owns="select2-attribute_values0-to-results" aria-activedescendant="select2-attribute_values0-to-result-d16b-111">
               <ul class="select2-selection__rendered" aria-live="polite" aria-relevant="additions removals" aria-atomic="true">
                  <li class="select2-search select2-search--inline"><input class="select2-search__field" type="text" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="textbox" aria-autocomplete="list" placeholder="Select terms" style="width: 418.797px;" aria-owns="select2-attribute_values0-to-results" aria-activedescendant="select2-attribute_values0-to-result-d16b-111"></li>
               </ul>
            </span>
    </span>
    <span class="dropdown-wrapper" aria-hidden="true"></span>
    </span>
    <button class="button plus select_all_attributes">Select all</button>
    <button class="button minus select_no_attributes">Select none</button>
    <button class="button fr plus add_new_attribute">Add new</button>
  </td>
</tr>

根据 HTML:

<select multiple="" data-placeholder="Select terms" class="multiselect attribute_values wc-enhanced-select select2-hidden-accessible enhanced" name="attribute_values[0][]" tabindex="-1" aria-hidden="true">

WebElement 显然是一个 <select> 节点并且 select 来自 tag you need to induce for the and you can use either of the following 的选项之一:

  • 使用 cssSelectorselectByVisibleText():

    new Select(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.multiselect.attribute_values.wc-enhanced-select.select2-hidden-accessible.enhanced[data-placeholder='Select terms']")))).selectByVisibleText("Adidas");
    
  • 使用 xpathselectByValue():

    new Select(new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[@class='multiselect attribute_values wc-enhanced-select select2-hidden-accessible enhanced' and @data-placeholder='Select terms']")))).selectByValue("110");