Can selenium handle autocomplete?

前端 未结 17 1311
粉色の甜心
粉色の甜心 2020-12-09 04:26

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

17条回答
  •  鱼传尺愫
    2020-12-09 05:21

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

提交回复
热议问题