猿问

如何使用 Selenium 和 Java 选择第一个自动建议

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


这是带有 TestNG 插件的 eclipse 氧气


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


网页代码:


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


交互式爱情
浏览 197回答 3
3回答

ibeautiful

1- 首先在搜索框中输入您的单词。2-然后等待,直到搜索元素可见或可单击。&nbsp;WebDriverWait wait = new WebDriverWait(driver, milliseconds);&nbsp;wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//li[@id='indexTab1']"))));3-然后单击所需的元素driver.findElement(By.xpath("//li[@id='indexTab1']")).click();

红颜莎娜

我认为,你的是单页应用程序。最有可能的是它的 Angular js 应用程序。这些 SPA 根据您在下拉编辑框中键入的内容不断更新/显示下拉列表中的选项。请尝试以下代码,driver.findElement(By.className("searchfilter")).sendKeys("Abilify");driver.findElement(By.Xpath("//li[contains(text(), 'Abilify')]")) 。点击() ; // 注意:如果存在多个匹配项,Selenium 始终对第一个元素起作用。您还可以参数化您的选择值,如下所示String temp=“能力”;driver.findElement(By.className("searchfilter")).sendKeys(temp);driver.findElement(By.Xpath("//li[contains(text()," + temp +")]")).click() ;

饮歌长啸

使用sendKeys()所需文本调用后,要选择第一个自动建议,您需要引发WebDriverWait,并且visibilityOfElementLocated()可以使用以下定位器策略之一:cssSelector:driver.findElement(By.className("searchfilter")).sendKeys("Abilify");new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("li.list-group-item-action[name='ABILIFY - ARIPIPRAZOLE'][href$='-ABILIFY']"))).click();xpath:driver.findElement(By.className("searchfilter")).sendKeys("Abilify");new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[contains(@class, 'list-group-item-action') and @name='ABILIFY - ARIPIPRAZOLE'][contains(., 'ABILIFY - ARIPIPRAZOLE')]"))).click();
随时随地看视频慕课网APP

相关分类

Java
我要回答