如何使用Selenium和Java从动态下拉列表中选择自动建议 [英] How to select the auto suggestion from the dynamic dropdown using Selenium and Java
问题描述
我正尝试以以下形式为主题字段选择值: https://demoqa.com/automation-practice-form
I am trying to select the value for Subjects field in the following form: https://demoqa.com/automation-practice-form
这是一个输入字段,可根据我们的输入动态提供建议,稍后我们需要从这些建议中选择值.我无法选择所需的值.
It is an input field that dynamically gives suggestions based on our input and later we need to select values from those suggestions. I am unable to select the desired value.
下面的代码仅填充输入区域,但未选择该值.
The Below code only populates the input area but the value is not selected.
driver.findElement(By.id("subjectsInput")).sendKeys("English");
driver.findElement(By.id("subjectsInput")).click(); //This line doesnot click on the desired value.
如何选择所需的值.请帮忙.
How to Select the desired value. Please help.
推荐答案
要调用,请单击唯一的自动建议 英语,您需要诱导定位器策略:
To invoke click on the only auto suggestion English you need to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies:
-
cssSelector
:
driver.get("https://demoqa.com/automation-practice-form");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#subjectsInput"))).sendKeys("English");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.subjects-auto-complete__menu"))).click();
xpath
:
driver.get("https://demoqa.com/automation-practice-form");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='subjectsInput']"))).sendKeys("English");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(@class, 'subjects-auto-complete__menu')]"))).click();
浏览器快照:
Browser Snapshot:
这篇关于如何使用Selenium和Java从动态下拉列表中选择自动建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!