I have a dynamic search box for a medication list. The list of medications will appear based on first 3 letters user entered: I have xpath for the box: //input[@placeholder='Search for a medication...']" But I could not locate medication list, since it's dynamic. I tried many different things and non of them are working. Please see the list below. Any suggestions are appreciated, thank you!
1st try
public void medSearch(String value) throws Throwable{
By medSearch = By.xpath("//input[@placeholder='Search for a medication...']");
// String medSearch = "TETTERINE, Topical Ointment";
if(waitForPresenceOfElement(medSearch, 20)){
type(medSearch,value);
moveToElementAndClick(medSearch);
}
}
2nd try
public void select_med()throws Throwable {
WebElement medSearch2 = Driver.findElement(By.xpath("//*[normalize-space(text()) and normalize-space(.)='View More Info'])[1]/following::input[1] "));
//if(waitForPresenceOfElement(medSearch2, 20)){
Select dropdown = new Select(medSearch2);
dropdown.selectByVisibleText("TETTERINE, Topical Ointment");
// dropdown.selectByValue("");
}
3rd try
public void med4()throws Throwable {
WebElement listbox = Driver.findElement(By.xpath("//input[@placeholder='Search for a medication...']"));
listbox.sendKeys("tett");
listbox.sendKeys(Keys.PAUSE);
listbox.sendKeys(Keys.DOWN);
listbox.sendKeys(Keys.ENTER);
}
4th try:
/* public void medSearch3(String value) throws Throwable{
WebElement listbox = Driver.findElement(By.xpath("//input[@placeholder='Search for a medication...']"));
WebElementWait wait = new WebElementWait(listbox, 1);
if(waitForPresenceOfElement(listbox, 20)){
type(value);
listbox.sendKeys(Keys.DOWN, Keys.RETURN);
}
} */
5th try
public void selectMedication (String first_3_letters, String text) throws Throwable {
//String text2 = null;
By medication_box = By.xpath("//input[@placeholder='Search for a medication...']");
if(waitForElementPresent(medication_box, 20)) {
selectBySendkeys(medication_box,first_3_letters, text);
//moveToElementAndClick();
emanager.Pass("Enter value in ':' text field", "Entered in field");
} else {
emanager.Failure("Enter value in ':'", "Unable to enter in field", Driver);
}
if(waitForElementPresent(medication_box, 20)) {
selectBySendkeys(medication_box,first_3_letters, text);
//moveToElementAndClick();
6th try:
public void selectmed() {
WebElement medSearch = Driver.findElement(By.xpath("//input[@placeholder='Search for a medication...']"));
Select dropdownXpath = new Select(medSearch);
List<WebElement>dropdown_list = dropdownXpath.getOptions();
int total_rx = dropdown_list.size();
System.out.println("Totalrx" +total_rx);
for(WebElement ele:dropdown_list) {
String rx_name = ele.getText();
System.out.println("rx list" + rx_name);
}
if(waitForPresenceOfElement(medSearch, 20)){
type(medSearch,value);
//moveToElementAndClick(medSearch);
sendKeys(Keys.ArrowDown);
}
7th try:
public void selectFromDropdown(String value) throws IOException, AWTException {
WebElement dropdown = Driver.findElement(By.xpath("//input[@placeholder='Search for a medication...']"));
Select dropdownXpath = new Select(dropdown);
List<WebElement>dropdown_list = dropdownXpath.getOptions();
int total_rx = dropdown_list.size();
System.out.println("Totalrx" +total_rx);
for(WebElement ele:dropdown_list) {
String rx_name = ele.getText();
System.out.println("rx list" + rx_name);
}
System.out.println("Selected " + value + " for Dropdown ");
if(dropdown.isDisplayed()) {
emanager.Pass("Verify whether user is able to select value from dropdown","User is able to select value from dropdown");
System.out.println("User is able to select value from dropdown");
dropdownXpath.selectByVisibleText(value);
} else {
emanager.Failure("Verify whether user is able to select value from dropdown", "User is not able to select value from dropdown", Driver);
System.out.println("User is not able to select value from dropdown");
assert(true);
}
}
Here is the HTML
<input _ngcontent-c0="" autocapitalize="off" autocomplete="off" autocorrect="off" class="completer-input form-control mbe-input ng-pristine ng-valid ng-touched" ctrinput="" type="search" name="" placeholder="Search for a medication..." maxlength="524288" tabindex="0" xpath="1">
I'm getting error if I'm using this statement Select dropdownXpath = new Select(dropdown); and error looks like this: "using select for input type" for all other methods it's able to type in but not selecting from drop down and just ignoring it and moving forward to the next method with no errors