0

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

Sureshmani Kalirajan
  • 1,938
  • 2
  • 9
  • 18
Elinka
  • 1
  • 2
  • 1
    can you add html source of the search box & results – Sureshmani Kalirajan Aug 07 '19 at 18:35
  • 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. – Elinka Aug 07 '19 at 18:48
  • Are you able to capture html code for the search results? – Sureshmani Kalirajan Aug 07 '19 at 18:54
  • unfortunately I'm not able to locate it, I can see the list of medications in Network tab as an array list, but if I do R Click inspect dropdown disappearing – Elinka Aug 07 '19 at 18:58
  • the problem is I need to select a medication from dropdown, because after selecting a new dropdown appearing below medication name (Select Strength) after I select strength one more dropdown will show to select member. – Elinka Aug 07 '19 at 19:02
  • I can think of 2 solutions here. i will update the results. – Sureshmani Kalirajan Aug 07 '19 at 19:11

1 Answers1

0

Try these 2 options.

#1 - Assuming the search result will be on the DOM, you can do a blind xpath search matching the search result text and select the element,

    WebElement SearchInput = driver.findElement(By.xpath("//input[@type='search']"));
  SearchInput.sendKeys("tett");
   Thread.sleep(2000);// remove this once you determine how to dynamically wait
  WebElement SearchResult = driver.findElement(By.xpath("//*[contains(text(),'TETTERINE')]"));
  SearchResult.click();

#2 - just use Arrown_Down key press to navigate the search results

SearchInput.sendKeys("tett");
            SearchInput.sendKeys(Keys.ARROW_DOWN);// move down the results list
            SearchInput.sendKeys(Keys.ENTER);
Sureshmani Kalirajan
  • 1,938
  • 2
  • 9
  • 18