I am struggling to get a hold of a dynamic drop down to run unit test . Can some suggest how to get hold of auto complete drop down. The moment I try to click on drop down to inspect , the drop down disappears. Therefore, i am unable to iterate over the options of drop-down . The URL is https://www.flydubai.com/en-gb/. my code is d.get(“https://www.flydubai.com/en-gb/”);
d.manage().window().maximize();
d.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Accept cookies
WebElement cookiesDialog = d.findElement(By.xpath("//div[contains(@class, 'osano-cm-dialog')]"));
WebElement acceptButton = cookiesDialog.findElement(By.xpath(".//button[contains(@class, 'osano-cm-accept-all')]"));
acceptButton.click();
// Select one-way option
WebDriverWait wait = new WebDriverWait(d, 10);
WebElement oneWay = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class="MuiTypography-root MuiTypography-body1 jss1 css-1l0ew3p"]")));
oneWay.click();
// Interact with the input field to trigger dropdown
WebElement fromInput = wait.until(ExpectedConditions.elementToBeClickable(By.id("from-airportsList")));
fromInput.click();
fromInput.sendKeys("Glasgow");
// Wait for suggestions to appear
WebDriverWait waitForSuggestions = new WebDriverWait(d, 10);
// Try to locate the dropdown suggestions based on the parent structure
List<WebElement> suggestions = waitForSuggestions.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(
By.xpath("//div[contains(@class, 'MuiAutocomplete-root')]//div[contains(@role, 'option')]")
));
// If suggestions are not found, try a more general approach
if (suggestions.isEmpty()) {
suggestions = waitForSuggestions.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(
By.xpath("//div[@role='listbox']//div[@role='option']")
));
}
// Iterate through suggestions and select "Glasgow"
boolean suggestionFound = false;
for (WebElement suggestion : suggestions) {
String suggestionText = suggestion.getText();
System.out.println("Suggestion: " + suggestionText);
if (suggestionText.contains("Glasgow")) {
suggestionFound = true;
suggestion.click();
break;
}
}
// Assert that the suggestion was found
Assert.assertTrue("Suggestion 'Glasgow' was not found in the dropdown.", suggestionFound);
i am trying to get hold of dropdown and iterate over to get all possible options i get for sendKeys(“Gla”)
Ankita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.