0

Script is selecting country element then entering Text in Search field as "India" which shows 2 values in search result . I am unable to select displayed field value ?

Test Case :- 1. Navigate to Country ( Please select ) 2. Search box is displayed - Example Sendkeys "India" 3. List of Countries displayed

HTML Code for Country Element ( Please select )

enter image description here

HTML Code for Search text box in dropdown enter image description here

//Dropdown & Search by Text India

    driver.findElement(By.xpath("//span[@id='ojChoiceId_country_selected']")).click();
    System.out.println("You are able to click on Dropdown");

    driver.findElement(By.xpath("//*[@id=\"oj-listbox-drop\"]/div/div/input")).sendKeys("United States");
  • can you share your drop down image? – Upkar Singh May 2 at 6:36
  • or share your DOM image – Upkar Singh May 2 at 6:38
  • have shared the DOM Image – Akshay Dongar May 2 at 6:44
  • It is an autocomplete text box – Upkar Singh May 2 at 6:50
  • these drop down options are under which tag? – Upkar Singh May 2 at 6:52
0

I have also got a autocomplete text box options, so for that i have just created the method which used to control it, may this help you please modify it with your use:-

public static void selectAutoSearchText(By selectBoxElement,By textBoxElement, By dropdownElement, String textValue, String matchingValue, WebDriver driver, WebDriverWait wait) throws InterruptedException
    {       
        try {
 wait.until(ExpectedConditions.visibilityOfElementLocated(selectBoxElement)).click();

            WebElement searchBox= wait.until(ExpectedConditions.visibilityOfElementLocated(textBoxElement));
            searchBox.sendKeys(textValue);
            Thread.sleep(1000);
            WebElement waitElement= wait.until(ExpectedConditions.visibilityOfElementLocated(dropdownElement));
            Thread.sleep(1000);
            List<WebElement> dropdownList= waitElement.findElements(By.tagName("tr"));
            //System.out.println(dropdownList);
            //System.out.println(dropdownList.size());
            for(WebElement listElement :dropdownList)
            {
                //System.out.println(listElement.getText());
                if(listElement.getText().equals(matchingValue))
                {
                    listElement.click();
                    break;
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }  

I have used both Explicit Wait and Thread.sleep just because by drop down options value loaded after some time, so in some cases it failed to select options so i used the thread.sleep to reduce it.

In this you just need to pass the Xpath of these values and text which you will enter and matching value is for select that country name on which you want click for select that option.

I have used tag name <tr> you can change it according to your options tag because it can be Button or Span tag also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.