I have a test case that requires typing in a partial value into an ajax based textfield and verifying the list has the expected content. If it does, select the content. An
I was able to solve this by using the below function: The below function takes the text you want to select as a parameter. Ex: If you want to select "javascript", just type "java" in your textbox & pass the text you want to select, in this specific case it is "javascript".
public void selectOptionWithText(String textToSelect) {
try {
//Add the below sleep if necessary
// Thread.sleep(1000);
WebElement autoOptions = driver.findElement(By.className("autocomplete"));
List optionsToSelect = autoOptions().findElements(By.tagName("div"));
for (WebElement option : optionsToSelect) {
if (option.getText().equals(textToSelect)) {
System.out.println("Trying to select: " + textToSelect);
option.click();
break;
}
}
}
catch(Exception e){
System.out.println("Error");
}
}