Search code examples
javaseleniumselenium-webdriverdisplayautosuggest

How to create a list of the autocomplete <li> elements when parent <ul> element contains the attribute style="display: none;" through Java Selenium


I'm using Java selenium to create a automation project. Now in our web app, there is address input box and has autocomplete feature. The problem is after input a partial address, we need click one of the option from the list. Here is the html: hrml ul list

Now I have tried to get the list. But failed:

WebElement autoCompelet = driver.findElement(By.xpath("/html/body/ul[1]"));
List<WebElement> options = autoCompelet.findElements(By.tagName("li"));
logger.debug(options.size());
for (WebElement option1 : options) {
    logger.debug(option1);
}

I can print out , but

  • list is empty. I've also tried to use wait method like:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/ul[1]/li[1]")));
    

    Unfortunately, none of them works. Is anyone has any idea on this? Any input is appreciated. Thank you guys.


  • Solution

  • By.className("li") is invalid,the value should be the class name,need to use By.tagName("li") instead.

    Also you can change your xpath to get ul by class and then get li directly

    WebElement autoCompelet = driver.findElement(By.xpath("//ul[@class='af_list']"));
    List<WebElement> options = autoCompelet.findElements(By.tagName("li"));
    logger.debug(options.size());
    for (WebElement option1 : options) {
        logger.debug(option1);
    }