首页 > 解决方案 > 如何使用 Selenium 和 Java 选择第一个自动建议

问题描述

我正在输入要搜索的名称,然后显示自动建议,但我无法从自动建议中选择第一个选项。

这是带有 TestNG 插件的日食氧气

driver.findElement(By.className("searchfilter")).sendKeys("Abilify");// This is working But after that option selection is not working

driver.findElement(By.cssSelector(".list-group-item:first-child")).click(); // Issue is here

html代码:

<li class="list-group-item list-group-item-action py-3 tabindex fs-1-1 bg-offwhite" id="indexTab1" href="970-ABILIFY" name="ABILIFY - ARIPIPRAZOLE">ABILIFY - ARIPIPRAZOLE</li>

标签: javaselenium-webdriverxpathcss-selectorsautosuggest

解决方案


1-首先在搜索框中输入您的单词。

2-然后等到您的搜索元素可见或可点击。

 WebDriverWait wait = new WebDriverWait(driver, milliseconds);
 wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//li[@id='indexTab1']"))));

3-然后单击所需的元素

driver.findElement(By.xpath("//li[@id='indexTab1']")).click();

推荐阅读