coolgokul
coolgokul

Reputation: 357

how to select jquery unorderded drop down list selenium webdriver

I'm able to write java code to select dropdown if it has normal select class something like :-

new Select(driver.findElement(By.id("someid"))).selectByValue("1"));

But below code has select2 class from jquery. Tried multiple options. I can select to show dropdown by from then, I couldn't select Apple or orange, or select grape.

can some one please help me on how to select Apple from this kind of dropdown list? Any clue or code is much appreciated. ..

this is how code looks like

<div id="select2-drop" class="select2-drop select2-display-none select2-with-searchbox select2-drop-active" style="somestyleelements">
    <div class="select2-search">
        <ul id="select2-results-1" class="select2-results" role="listbox">
            <li class="select2-results-dept-0 select2-result select2-result-selectable select2-highlighted" role="presentation">
                <div id="select2-result-label-random" class="select2-result-label" role="option">
                    <span class="select2-match"></span>
                        Apples
                </div>
            </li>
            <li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation">
                <div id="select2-result-label-random" class="select2-result-label" role="option">
                    <span class="select2-match"></span>
                        Orange
                </div>  
            <li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation">
            <li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation">
            <li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation">
        </ul>
</div>

Upvotes: 2

Views: 2470

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23815

Select() class works only with <select> tag element. So you can't use Select() class here. You should try as below :-

WebDriverWait wait = new WebDriverWait(driver, 10);

//First click on dropdown to show options 
WebElement dropdown = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("select2-drop")));
dropdown.click();

//Now find desired option and click 
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//li[normalize-space(.) = 'Apples']"))).click();

Upvotes: 1

Related Questions