硒 - 选择下拉菜单。无法找到元素

时间:2017-07-06 14:35:03

标签: selenium-webdriver selenium-chromedriver

我无法使用以下代码选择下拉选项。此代码返回错误,因为元素未找到。请协助。谢谢!

    WebElement mySelectElement = driver.findElement(By.id("incident.severity"));
    Select dropdown= new Select(mySelectElement);
    dropdown.selectByVisibleText("3 - Medium");
  

线程中的异常"线程-2"   org.openqa.selenium.NoSuchElementException:没有这样的元素:无法   定位元素:{"方法":" id","选择器":" incident.severity"}

下面是下拉屏幕截图和相应的代码。

field screenshot



<div class="form-group " style="" id="element.incident.severity">
 <div nowrap="true" data-type="label" id="label.incident.severity" 
      type="choice" choice="3" class="foreign">
 <label dir="ltr" class=" col-xs-12 col-md-3 col-lg-4 control-label" 
       onclick="return labelClicked(this);" for="incident.severity"><span 
      id="status.incident.severity" title="" mandatory="false" aria-label="" 
     class=" label_description" oclass="" data-original-title=""></span>
<span title="" class="label-text" data-html="false" data-original-
title="">Severity</span></label></div>
  
<div class="col-xs-10 col-sm-9 col-md-6 col-lg-5 form-field input_controls">
<input name="sys_original.incident.severity" 
       id="sys_original.incident.severity" type="hidden" value="4">
<select style="direction:ltr; " id="incident.severity" 
      onchange="onChange('incident.severity');" class="form-control" ng-non-
      bindable="true" name="incident.severity" mandatory="false">
        <option value="1">1 - Critical</option>
        <option value="2">2 - High</option>
        <option value="3">3 - Medium</option>
        <option value="4" selected="SELECTED">4 - Low</option>
</select></div>
      <div class="col-xs-2 col-sm-3 col-lg-2 form-field-addons"></div>
    </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

当驱动程序无法在网页的DOM中找到webelement时,会发生

NoSuchElementException

出现此类错误的原因之一是您的页面未完全加载,并且您正在尝试查找该元素,尝试在搜索webelement之前等待一段时间。对于等待,您可以使用&#34;隐式等待&#34;

示例代码:

//Initializing a WebDriverWait instance with a wait time of 30 seconds.
WebDriverWait wait = new WebDriverWait(driver,30); 

//This code will wait till 30 seconds(Maximum) before throwing the exception.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("incident.severity")));

WebElement mySelectElement = driver.findElement(By.name("incident.severity"));

如果有帮助,请告诉我。