如何使用java从selenium Webdriver中的List获取下拉值

时间:2016-09-05 09:03:40

标签: java html sql selenium selenium-webdriver

如何将列表值视为下拉值?

如何从列表中选择编辑下拉值...

KmlContainer = layer.getContainers().iterator().next();
if (kmlContainer == null) return;

for (KmlPlacemarks placemark : kmlContainer.getPlacemarks()) {
  // Do the placemark magic here!
}

2 个答案:

答案 0 :(得分:0)

您应该尝试使用By.cssSelector()获取列表中的所有选项,如下所示: -

WebDriverWait wait = new WebDriverWait(driver, 10);

//First click on select div 
WebElement select = wait.until(ExepectedConditions.elementToBeClickable(By.cssSelector("div.nice-select.demoBasic")));
select.click();

//Not get all nested options 
List<WebElement> options = wait.until(ExepectedConditions.presenceOfNestedElementsLocatedBy(select, By.cssSelector("ul.list li.option")));

//Now iterate to fetch all options and select edit option
for(WebElement option : options)
{
  if(option.getText().equals("Edit"))
  {
      option.click();
  }
}

或者您可以使用By.xpath()在一行中根据文字选择一个选项,如下所示: -

wait.until(ExepectedConditions.elementToBeClickable(By.xpath(".//li[text() = 'Edit']"))).click();

答案 1 :(得分:0)

我得到了解决方案......

    driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/table/tbody/tr[3]/td[3]/div")).click();
    driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
    Thread.sleep(4000);
    driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/table/tbody/tr[3]/td[3]/div/ul/li[1]")).click();
    driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
    Thread.sleep(4000);