2

I'm new selenium webdriver using Java. I'm able 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>

Thanks in advance

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
coolgokul
  • 257
  • 1
  • 7
  • 18

1 Answers1

1

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();
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • 1
    Worked like charm. Much thanks.!! Can you please shed some light on this part of code. Just trying to understand more and sorry if it's elementary... ".//li[normalize-space(.) - What does this normalize-space does and why there is dot? – coolgokul Sep 23 '16 at 02:41
  • It just xpath function which is use to avoid extra whitespace for text content of an element, and dot means it just match the exact text content inside any element no matter how many other elements present inside desire element, for more details follow this link https://www.w3.org/TR/xpath/#function-normalize-space – Saurabh Gaur Sep 23 '16 at 03:12
  • @coolgokul To know more about dot [have a look here](http://stackoverflow.com/questions/38240763/xpath-difference-between-dot-and-text). Thanks – Saurabh Gaur Sep 23 '16 at 03:17