简体   繁体   English

动态下拉搜索栏结果未解析为我的 Selenium 列表

[英]Dynamic Drop-down search bar results do not parse into my List for Selenium

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.我正在创建一个简单的脚本来访问priceline.com ,然后在Departing Flights下搜索“ N ”并从下拉列表中选择New York City (NYC)

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.预期:我预计不同机场的departureDropdown的长度为 9。 (Nadi, New York City, Nagasaki, etc) and expect departureDropdown[i] to return one of the city names in plain text. (南迪、纽约市、长崎等)并期望departureDropdown[i]以纯文本形式返回城市名称之一。

Actual: My code is stuck at the list initialization.实际:我的代码卡在列表初始化中。 departureDropdown is empty. departureDropdown为空。

You have used By.id but you have created xPath.您已经使用了 By.id 但您已经创建了 xPath。

Try with below xPath尝试使用以下 xPath

List<WebElement> departureDropdown = driver.findElements(By.xPath("//div[@id='flight-departure-airport0-dropdown']//div[@role='option']"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM