无法从webdriver的下拉列表中单击2值

时间:2016-08-30 09:33:00

标签: selenium webdriver

守则是:

public void setRing(int index, String ringPattern) throws InterruptedException {
List<WebElement> webElementList = driver.findElements(By.xpath(an.getProperty("an_ringPattern")));

webElementList.get(index).click();

List<WebElement> options = webElementList.get(index).findElements(By.tagName("option"));
  for (WebElement element : options) {
      if (element.getText().equals(ringPattern)) {
        element.click();
        Thread.sleep(2000);
      }
   }
}

当我在调试模式下执行此代码时,它工作正常。它选择我传递给方法的任何值。 但是当我运行此代码时,它无法更改下拉列表中的值。它选择下拉列表中的值,但无法设置该值。

如果我错过了什么,请告诉我。

2 个答案:

答案 0 :(得分:1)

如果抛出异常.Error message display below at org.openqa.selenium.support.ui.WebDriverWait.timeoutExceptio‌​n(WebDriverWait.java‌​:80),意味着该元素在框架上没有准备就绪,驱动程序不在该框架上或xpath错误。 您可以尝试切换到元素所在的框架,如下所示:driver.switchToFrame("here goes the id of the frame");您可以检查框架的ID,或者有时只传递整数0的工作。另外,我宁愿使用wait1.until(ExpectedConditions.visibilityOfElementLocated(element));代替wait1.until(ExpectedConditions.presenceOfElementLocated(element));。 当您使用presenceOfElementLocated时,您无法确保该元素对于驱动程序是可见的。

答案 1 :(得分:0)

您可以在selenium中使用显式等待 - WebDriverWait

使用以下修改后的代码

public void setRing(int index, String ringPattern) throws InterruptedException {
List<WebElement> webElementList = driver.findElements(By.xpath(an.getProperty("an_ringPattern")));

WebDriverWait wait = new WebDriverWait(driver, 60); 
wait.until(ExpectedConditions.presenceOfElementLocated(webElementList.get(index)));
webElementList.get(index).click();

List<WebElement> options = webElementList.get(index).findElements(By.tagName("option"));
  for (WebElement element : options) {
      WebDriverWait wait1 = new WebDriverWait(driver, 60);  
      wait1.until(ExpectedConditions.presenceOfElementLocated(element));
      if (element.getText().equals(ringPattern)) {
        element.click();
        Thread.sleep(2000);
      }
   }
}