Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

select2 drop down with selenium

I have a drop down in WordPress products page which is a select2 ajax enabled.

I have managed to show the options in the drop down using selenium.

But i am not able to select one of the options from the list.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am able to get the element using the below code. But the element is not castable to Select nor clickable.

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

Any ideas how to select any option from it?

The HTML code for the drop down is

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

>Solution :

As per the 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">

The WebElement is clearly a <select> node and to select one of the options from the tag you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • Using cssSelector and selectByVisibleText():

    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");
    
  • Using xpath and selectByValue():

    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");
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading