【问题标题】:Selenium-webdriver help needed with selecting dropdown without select option在没有选择选项的情况下选择下拉菜单需要 Selenium-webdriver 帮助
【发布时间】:2017-06-15 17:52:16
【问题描述】:

我正在尝试在下拉列表中选择一个选项。我无法使用选择,因为我没有选择 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>

我在 c# 中使用 selenium webdriver。

【问题讨论】:

  • 一旦你得到了下拉 webelement aka CountryDropdownElement..,使用它并创建一个选择实例,然后像往常一样按索引/值选择选项
  • 我使用了 selectElement.SelectByText(selectedvalue) 我收到以下错误:元素不可见:元素当前不可见,可能无法操作
  • 能否给出完整的select代码?
  • 我添加了完整的选择代码。
  • 点击下拉后,尝试点击这个元素(xpath - "//li[contains(text(),'Belgie')]"

标签: c# selenium


【解决方案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;

}

【讨论】:

  • 对于您的代码: List lstOptions= driver.findElements(By.xpath("//ul[contains(@class,'selectpicker')]/a/span"));我尝试在 C# 中这样做: private IWebElement NewCountryWithinDropdown(string dutchCountryName) { return BrowserFactory.Driver.FindElement(By.XPath(string.Format("//ul[contains(@class,'selectpicker')]/ a/span, '{0}' ", dutchCountryName)));我收到以下错误:无效选择器:无法使用 xpath 表达式定位元素 //ul[contains(@class,'selectpicker')]/a/span,'Belgie'
  • 请尝试以下私有 IWebElement NewCountryWithinDropdown(string dutchCountryName) { return BrowserFactory.Driver.FindElement(By.XPath(string.Format("//‌​ul[contains(@class,'‌​ selectpicker')]/a/sp‌​an[text()='{0}']", dutchCountryName)));}
  • 谢谢。但我收到以下错误:附加信息:无效选择器:无法使用 xpath 表达式定位元素 //‌​‌​ul[contains(@class‌​,'‌​selectpicker')]/‌​a/sp‌​ an[text()='Belgie‌​'] 因为以下错误:SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//‌​‌​ul[contains(@class‌​,'‌​ selectpicker')]/‌​a/sp‌​an[text()='Belgie‌​']' 不是有效的 XPath 表达式。
  • 您可以尝试在 xpath 中使用双引号而不是单引号
  • 对不起,我在 xpath 中错过了 li。尝试以下。 //‌​‌​ul[contains(@class‌​,'‌​selectpicker')]/‌​li/a/sp‌an[text()='{0}']"。我已在答案部分更新为好吧。
【解决方案2】:

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

不是完全匹配,但显示了它是如何完成的: Nested Hover in Selenium C#

【讨论】:

  • 我正在努力如何检索价值所在的位置。例如,我想选择比利时。如何检索此值的位置。在您使用的链接中: Actions builder = new Actions(ngDriver); var elementToClick = ngDriver.FindElement(By.ClassName("dpcontract"));
  • 您将在按钮上执行单击操作,以便项目(下拉列表)可见。如果您随后运行您的代码,它应该能够找到该元素。 (参考您得到的错误:元素不可见:元素当前不可见,可能无法操作)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 2014-02-26
  • 2018-02-17
  • 2018-02-04
  • 2018-12-12
相关资源
最近更新 更多