How to handle WebElement Dropdown whose tag starts with <Span>?



  • 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");
    


  • 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.



Suggested Topics