1

I am trying to select an option within a dropdown. I am not able to use a select since I dont have a select ID. Because then I get the message expected select got button.

I am able to open the dropdown with:

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

My code looks as follow:

<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>

In am using selenium webdriver with c#.

6
  • once you have got the dropdown webelement aka CountryDropdownElement.., use this and create a select instance and then select the option by index/value as usual Commented Jun 15, 2017 at 8:27
  • I used selectElement.SelectByText(selectedvalue) I receive the following error: element not visible: Element is not currently visible and may not be manipulated Commented Jun 15, 2017 at 8:41
  • can you give the complete code for the select? Commented Jun 15, 2017 at 8:42
  • I added the complete code for select. Commented Jun 15, 2017 at 8:47
  • after clicking on drop down, try to click this element(xpath - "//li[contains(text(),'Belgie')]" Commented Jun 15, 2017 at 8:50

2 Answers 2

1

Please try the following code. I have it in 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;

}
Sign up to request clarification or add additional context in comments.

12 Comments

For your code: List<WebElement> lstOptions= driver.findElements(By.xpath("//ul[contains(@class,'selectpicker')]/a/span")); I tried it to do it in C# like this: private IWebElement NewCountryWithinDropdown(string dutchCountryName) { return BrowserFactory.Driver.FindElement(By.XPath(string.Format("//ul[contains(@class,'selectpicker')]/a/span, '{0}' ", dutchCountryName))); } I get the following error: invalid selector: Unable to locate an element with the xpath expression //ul[contains(@class,'selectpicker')]/a/span, 'Belgie'
please try the following private IWebElement NewCountryWithinDropdown(string dutchCountryName) { return BrowserFactory.Driver.FindElement(By.XPath(string.Format("//‌​ul[contains(@class,'‌​selectpicker')]/a/sp‌​an[text()='{0}']", dutchCountryName)));}
Thanks. But I receive the following error: Additional information: invalid selector: Unable to locate an element with the xpath expression //‌​‌​ul[contains(@class‌​,'‌​selectpicker')]/‌​a/sp‌​an[text()='Belgie‌​'] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//‌​‌​ul[contains(@class‌​,'‌​selectpicker')]/‌​a/sp‌​an[text()='Belgie‌​']' is not a valid XPath expression.
You can try with double quotes instead of single in the xpath
sorry i missed li in the xpath. try the following. //‌​‌​ul[contains(@class‌​,'‌​selectpicker')]/‌​li/a/sp‌an[text()='{0}']". I have updated in the answer section as well.
|
0

You'll first have to click the button so the item is visible. This can be done through an action. See for a reference on how to build click actions.

Not an exact match but shows how it could be done: Nested Hover in Selenium C#

2 Comments

I am struggling how to retrieve where the value is. For example I want to select Belgie. How do I retrieve where this value is. In your link you are using: Actions builder = new Actions(ngDriver); var elementToClick = ngDriver.FindElement(By.ClassName("dpcontract"));
You'll have the do a click action on the button so the item(drop down list) is visible. If you then run your code, it should be able to find the element. (in reference to the error you were getting: element not visible: Element is not currently visible and may not be manipulated)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.