I'm creating a simple script to visit priceline.com and then searching " N " under Departing Flights and selecting New York City (NYC) from the dropdown list.
My code successfully types into the search bar but is still unable to find the dynamic dropdown with all the relevant results. I am not sure why.
// Clicks on "Departing from?" textbox and clears it before typing 'N'
driver.findElement(By.id("flight-departure-airport0")).click();
driver.findElement(By.id("flight-departure-airport0")).clear();
driver.findElement(By.id("flight-departure-airport0")).sendKeys("N");
// Store all dynamic search results into a list
List<WebElement> departureDropdown = driver.findElements(By.id("//*['flight-departure-airport0-dropdown']/div/div/div"));
System.out.println("List: "+departureDropdown);
Expected: I expected departureDropdown
to have a length of 9 with various airports. (Nadi, New York City, Nagasaki, etc) and expect departureDropdown[i]
to return one of the city names in plain text.
Actual: My code is stuck at the list initialization. departureDropdown
is empty.
You have used By.id but you have created xPath.
Try with below xPath
List<WebElement> departureDropdown = driver.findElements(By.xPath("//div[@id='flight-departure-airport0-dropdown']//div[@role='option']"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.