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();

Bence Kaulics
1,00712 silver badges22 bronze badges
asked Jan 4, 2019 at 9:28
1
  • Could you detail better the target DOM structure? From these two span, one cannot properly understand how to insert the text. Commented Jan 5, 2019 at 17:46

1 Answer 1

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.