Selenium-webdriver帮助选择下拉列表而不选择选项

时间:2017-06-15 08:21:47

标签: c# selenium

我正在尝试在下拉列表中选择一个选项。我无法使用选择,因为我没有选择ID。因为那时我得到了预期的消息选择了按钮。

我可以打开下拉列表:

    [FindsBy(How = How.XPath, Using = "//button[@data-id='Product_Contractor_Person_Contact_Country']")]
    private IWebElement CountryDropdownElement { get; set; }

我的代码如下:

<div class="col-lg-4 form-group">
   <label>
   Land
   </label>
   <label class="select select-block">
      <select class="selectpicker" data-width="100%" id="Product_Contractor_Person_Contact_Country" name="Product.Contractor.Person.Contact.Country" style="display: none;">
         <option selected="selected" value="NL">Nederland</option>
         <option value="BE">Belgie</option>
         <option value="FR">Frankrijk</option>
         <option value="UK">Engeland</option>
         <option value="DE">Duitsland</option>
         <option value="SE">Zweden</option>
         <option value="ES">Spanje</option>
         <option value="IT">Italie</option>
         <option value="ZA">Zuid Afrika</option>
         <option value="PL">Polen</option>
         <option value="UA">Oekraïne</option>
         <option value="ID">Indonesië</option>
         <option value="HU">Hongarije</option>
         <option value="PT">Portugal</option>
      </select>
      <div class="btn-group bootstrap-select" style="width: 100%;">
         <button type="button" class="btn dropdown-toggle selectpicker btn-default" data-toggle="dropdown" data-id="Product_Contractor_Person_Contact_Country" title="Nederland"><span class="filter-option pull-left">Nederland</span>&nbsp;<span class="caret"></span></button>
         <div class="dropdown-menu open">
            <ul class="dropdown-menu inner selectpicker" role="menu">
               <li rel="0" class="selected"><a tabindex="0" class="" style=""><span class="text">Nederland</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="1"><a tabindex="0" class="" style=""><span class="text">Belgie</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="2"><a tabindex="0" class="" style=""><span class="text">Frankrijk</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="3"><a tabindex="0" class="" style=""><span class="text">Engeland</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="4"><a tabindex="0" class="" style=""><span class="text">Duitsland</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="5"><a tabindex="0" class="" style=""><span class="text">Zweden</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="6"><a tabindex="0" class="" style=""><span class="text">Spanje</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="7"><a tabindex="0" class="" style=""><span class="text">Italie</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="8"><a tabindex="0" class="" style=""><span class="text">Zuid Afrika</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="9"><a tabindex="0" class="" style=""><span class="text">Polen</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="10"><a tabindex="0" class="" style=""><span class="text">Oekraïne</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="11"><a tabindex="0" class="" style=""><span class="text">Indonesië</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="12"><a tabindex="0" class="" style=""><span class="text">Hongarije</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
               <li rel="13"><a tabindex="0" class="" style=""><span class="text">Portugal</span><i class="glyphicon glyphicon-ok icon-ok check-mark"></i></a></li>
            </ul>
         </div>
      </div>
   </label>
</div>

我在使用selenium webdriver和c#。

2 个答案:

答案 0 :(得分:1)

请尝试以下代码。我在java中有它。

    //click on dropdown
    driver.findElement(By.xpath("//button[@data-id='Product_Contractor_Person_Contact_Country']")).click();

    //select option
    List<WebElement> lstOptions= driver.findElements(By.xpath("//ul[contains(@class,'selectpicker')]/li/a/span"));

    selectOption(lstOptions, "Zweden");

    public boolean selectOption(List<WebElement> lstOptions,String option){

    boolean isOptionAvailable=false;

    for(WebElement eleOptions:lstOptions){
        if(eleOptions.getText().trim().equals(option.trim())){
            isOptionAvailable=true;
            eleOptions.click();
            break;
        }
    }

    return  isOptionAvailable;

}

答案 1 :(得分:0)

您首先必须单击按钮才能看到该项目。 这可以通过一个动作来完成。请参阅有关如何构建单击操作的参考。

不完全匹配,但展示了如何完成: Nested Hover in Selenium C#