3
<span class="select2-selection__rendered" id="select2-office_id-container" title="--select--">--select--</span>
<span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span>

I am trying with below code, but unable to select dropdown


WebElement e1=driver.findElement(By.xpath("//*[@id='select2-office_id-container']"));
Actions a1=new Actions(driver);
a1.click(e1).sendKeys("Office of the Civil Registrar-cum-Sub Registrar,Sattari", Keys.ENTER).build().perform();

  • Could you detail better the target DOM structure? From these two span, one cannot properly understand how to insert the text. – João Farias Jan 5 at 17:46
1

Try this and let me know (modify the xpath and the values as per your code) -- I've made this to handle similar dropdown:

Thread.sleep(4000);
String options = driver.findElement(By.xpath("//span[@class='select2- results']")).getText();
System.out.println(options);

for (int i = 0; i < options.length(); i++) {
   WebElement cityName = driver.findElement(By.xpath("//span[@class='select2-results']/ul/li[" + (i+1) + "]"));
   try{
       if (cityName.getText().equals("Chandigarh")) {
            cityName.click();
            break;
       }
   }catch (Exception e){
       System.out.println("ABC");
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.