如何在自动下拉列表中显示所有值

时间:2017-02-26 06:39:56

标签: java selenium web selenium-webdriver webdriver

使用<div>标记开发时,如何使用selenium web驱动程序显示自动下拉列表中的所有值。

我的代码

driver.get("https://www.yatra.com/");// URl of yatra
String origin ="(//input[@name='flight_origin_city'])[1]";// text box for entering origin
driver.findElement(By.xpath(origin)).sendKeys("Bangalore");
String destination = "(//input[@name='flight_destination_city'])[1]";// text for entering destination
driver.findElement(By.xpath(destination)).sendKeys("M");
List<WebElement> lists= driver.findElements(By.xpath("(//div[@class='viewport'])[2]")); // this is the div where all the values are present.

for (int i=0; i<lists.size();++i){
    System.out.println(lists.get(i).getText());// getting the text for all the values.
}

2 个答案:

答案 0 :(得分:0)

xpath "(//div[@class='viewport'])[2]"为您提供单个元素,而不是列表。这也不是存储选项的位置,它是存储它们的容器的父级。尝试

 List<WebElement> lists= driver.findElements(By.xpath("(//div[@class='overview'])[3]//li"));

答案 1 :(得分:0)

你的案子有点困难,但这是值得探索的原因。这是对问题的逐步解剖:

桌面通知对话框

首先,当我尝试在Chrome中运行您的测试时,&#34;您是否要允许桌面通知?&#34; 对话框干扰测试(它弹出在第二个sendKeys之后的某个时间,这就是为什么文本框失去了焦点,其中一个条目被接受,这使得无法找到其他建议。)

我使用此提示解决了这个问题:Disable Chrome notifications (Selenium)

找到合适的容器

通过确定您可以将建议的容器定位为By.xpath("(//div[@class='viewport'])[2]"),您已经相当远了。但是,从那里开始,您需要更具体地考虑正确的子元素。此外,由于这是动态内容,因此建议使用显式的wait语句,以确保在元素加载时间过长时,测试不会过早失败。

我更改了您的测试,以便查看<li>元素,这些元素是具有<div>类的viewport元素的子元素。这些元素有孩子,相应地格式化城市,机场和国家名称。要获得这些文本,您必须深入了解DOM结构。以下是解决方案:

WebElement list = new WebDriverWait(driver, 2000).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//div[@class='viewport'])[2]")));

List<WebElement> elements = list.findElements(By.tagName("li"));

for (WebElement element : elements) {    
        String city = element.findElement(By.className("ac_cityname")).getText();
        String airport = element.findElement(By.className("ac_airportname")).getText();
        String country = element.findElement(By.className("ac_country")).getText();

        System.out.println(city + " " + airport + " (" + country + ")");
}

到目前为止,这么好。但请注意,此解决方案为您提供了以下输出:

Mumbai (BOM) Chatrapati Shivaji (India)
Madras (MAA) Chennai (India)
Chennai (MAA) Chennai (India)
Madurai (IXM)  (India)
  ()
  ()
  ()
  ()
  ()
  ()

滚动

这是因为您网页的另一个特色:它只呈现实际在视图中的元素的文本。因此,除了上面的代码之外,应用此技巧以确保在检索文本之前滚动到所有元素:Scroll Element into View with Selenium

长话短说:这是我的最终版本:

driver.get("https://www.yatra.com/");// URl of yatra
String origin ="(//input[@name='flight_origin_city'])[1]";// text box for entering origin
driver.findElement(By.xpath(origin)).sendKeys("Bangalore");
String destination = "(//input[@name='flight_destination_city'])[1]";// text for entering destination
driver.findElement(By.xpath(destination)).sendKeys("M");

WebElement list = new WebDriverWait(driver, 2000).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("(//div[@class='viewport'])[2]")));

List<WebElement> elements = list.findElements(By.tagName("li"));

for (WebElement element : elements) {
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

        String city = element.findElement(By.className("ac_cityname")).getText();
        String airport = element.findElement(By.className("ac_airportname")).getText();
        String country = element.findElement(By.className("ac_country")).getText();

        System.out.println(city + " " + airport + " (" + country + ")");
}

在Google Chrome中运行时,应该会为您提供以下输出:

Mumbai (BOM) Chatrapati Shivaji (India)
Madras (MAA) Chennai (India)
Chennai (MAA) Chennai (India)
Madurai (IXM) Madurai (India)
Mangalore (IXE) Bajpe (India)
Mysore (MYQ) Mysore (India)
Coorg (MYQ) (nearest airport Mysore) (India)
Male (MLE) Male (Maldives)
Melbourne (MEL) Tullamarine (Australia)
Mauritius (MRU) Plaisancet (Mauritius)