Home >Java >javaTutorial >How to Select Auto-Suggested Options from Dynamic Dropdowns Using Selenium and Java?

How to Select Auto-Suggested Options from Dynamic Dropdowns Using Selenium and Java?

Linda Hamilton
Linda HamiltonOriginal
2024-12-20 14:24:10327browse

How to Select Auto-Suggested Options from Dynamic Dropdowns Using Selenium and Java?

Selecting Auto-Suggestion from Dynamic Dropdown with Selenium and Java

When populating fields with dynamic auto-suggestions, it can be challenging to select the desired value. In this scenario, we aim to select the "English" option from the "Subjects" field on the DemoQA practice form.

The initial code snippet populates the input field with "English" but fails to select the value. To rectify this issue, we slightly modify the implementation. The updated code sequentially performs the following steps:

  1. Locate the "Subjects" input field by its ID.
  2. Send "English" to the input field.
  3. Press the down arrow key to move to the first suggestion (in this case, "English").
  4. Press the enter key to select the highlighted suggestion.
WebDriver Driver = new ChromeDriver();
Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String url = "https://demoqa.com/automation-practice-form";
Driver.get(url);
WebElement products = Driver.findElement(By.id("subjectsInput"));
products.sendKeys("English");
products.sendKeys(Keys.ARROW_DOWN);
products.sendKeys(Keys.ENTER);

This code successfully populates the "Subjects" field with "English" and selects it from the auto-suggestion dropdown.

The above is the detailed content of How to Select Auto-Suggested Options from Dynamic Dropdowns Using Selenium and Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn