<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();
Bence Kaulics
1,00712 silver badges22 bronze badges
asked Jan 4, 2019 at 9:28
-
Could you detail better the target DOM structure? From these two span, one cannot properly understand how to insert the text.João Farias– João Farias2019年01月05日 17:46:10 +00:00Commented Jan 5, 2019 at 17:46
1 Answer 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");
}
}
Bence Kaulics
1,00712 silver badges22 bronze badges
answered Jan 4, 2019 at 16:48