下拉列表不会在可见时选择值

时间:2018-04-04 17:18:11

标签: java selenium testing selenium-webdriver automation

当您选择家庭房间时,您会得到2个下拉菜单: 1.询问成年人数量 2.询问孩子的数量 从第二个下拉列表中选择一个数字后,第三个下拉列表即可显示。 为此,我使用显式等待,但仍然没有被选中,不知道为什么?

网站= https://www.trivago.in/?aDateRange[arr]=2018-06-16&aDateRange[dep]=2018-06-20&aPriceRange[to]=0&aPriceRange[from]=0&iPathId=84738&aGeoCode[lat]=10.850516&aGeoCode[lng]=76.27108&iGeoDistanceItem=0&aCategoryRange=0%2C1%2C2%2C3%2C4%2C5&aOverallLiking=1%2C2%2C3%2C4%2C5&sOrderBy=relevance%20desc&bTopDealsOnly=false&iRoomType=9&aRooms[0][adults]=2&aRooms[0][children][]=1&cpt=8473803&iIncludeAll=0&iViewType=0&bIsSeoPage=false&bIsSitemap=false&

  @Test
  public void familyRoomFeature() throws InterruptedException
  {
      //Selecting Family Room from the dropdown
      driver.findElement(By.xpath(".//*[@id='js-fullscreen-hero']/div/form/div[2]/div/div/ul/li               [3]/button")).click();
      Thread.sleep(7000);
      //Selecting Adult age & there is another dropdown where it asks how many kids a user would have
      Select adultSelect=new Select(driver.findElement(By.xpath(".//*[@id='select-num-adults-1']")));
      Select childSelect=new Select(driver.findElement(By.xpath(".//*[@id='select-num-children-1']")));     
      adultSelect.selectByIndex(2);
      childSelect.selectByIndex(1); 
          //So once you select Adult & child selection, another selection dropdown gets visible
      //I get an error here and it doesn't select the child's age inspite of explicit wait
      WebElement childAge =driver.findElement(By.id("select-ages-children-1-78"));
      WebElement childAgedrop = wait.until(ExpectedConditions.visibilityOf(childAge));
      Select childAgeFromSelect = new Select(childAgedrop);
      childAgeFromSelect.selectByIndex(3);
  }
}

2 个答案:

答案 0 :(得分:1)

显式等待是指您已经尝试查找子年龄元素。删除该行和下一行,并使用visibilityOfAllElementsLocatedBy(By locator)预期条件。

此外,定位器中使用的id是动态生成的,并且在下次运行时将失败。使用像这样的xpath定位器 - "//select[starts-with(@id,'select-ages-children')]",它将返回子年龄段的列表。

答案 1 :(得分:0)

当下拉列表相互依赖时,即如果仅在选择前一个元素后在下拉列表中填充选项,则可以使用以下等待。

new WebDriverWait(driver, 15).until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("//select[@id='select-ages-children-1-78']/option"), number))

''在上面的代码是可变的。一些下拉列表显示没有文字。在这种情况下,您可以将其设置为0.有些人只有一个' - 选择 - '选项。在这种情况下,您可以将其设置为1.