I wrote a for loop code that can access a dropdown list. I'm able to print the list but the code is unable to click on the specified option

public void userCapturesAccountDetails()  
    WebDriver driver = contextSteps.getDriver();
    List<WebElement> dropdown = driver.findElements(By.xpath("//ul[@class='dropdown-menu inner selectpicker']/li/a/span"));

    for(int i = 0; i < dropdown.size(); i++)
        WebElement element = dropdown.get(i);
        String innerhtml = element.getAttribute("innerHTML");

        System.out.println("values from dropdown is ======= "+innerhtml);


<div class="modal-dialog modal-lg" style="margin-top: 130.5px;">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close"></button>
<h4 class="modal-title">Add New Account</h4>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-8">...</div>
<div class="col-md-16 has-success">
<select class="bootstrap-select data-size="10" id="idSbxPractice" data-live-search="true" tabindex="1" Style="display: none;"
<span> class="text" title="SelectOption">SelectOption</span>
  • Can you add the error/exception you are getting when attempting to click the element? – VanderLinden Nov 15 '17 at 15:33
  • 2
    What you've posted appears to be an XPath; can you please post the actual HTML for the dropdown list? It will help to determine the best way to uniquely identify your target element. – VanderLinden Nov 15 '17 at 19:10

Since the tag name is select, try using the following:

Select _select = new Select(driver.findElement(By.cssSelector("#idSbxPractice")));
try {
} catch (Exception e) {
    // in case "OptionSelect" is not present in the dropdown list.
    System.out.println("values from dropdown is ======= "+innerhtml);

This will remove the need for a ''for'' loop.

| improve this answer | |
  • This looks likely to be the best bet based on the updated HTML. I would also note that the visible test for that option is "SelectOption", not "OptionSelect", which was included both here and in the code provided in the question. – VanderLinden Nov 16 '17 at 17:07
  • 1
    Also, since the <select> element has an @id attribute, you can locate it thusly: driver.findElement.byId("idSbxPractice") – VanderLinden Nov 16 '17 at 17:08

Depending on the WebDriver (Chrome vs Firefox, etc), you may find that you have to click the anchor rather than the span. Try adjusting your XPath to the following.

//ul[@class='dropdown-menu inner selectpicker']/li/a

Note that this means you may also have to adjust your option string matching logic. Rather than using innerHtml, you may have to access the element text, or find some other attribute that allows you to uniquely identify the desired element.


The HTML you added to the question does not seem to match the code you wrote at all. For example, you are trying to locate an element of type <button>, and there are no buttons in the HTML. The case is similar for the list elements.

| improve this answer | |
  • Clicking on a link instead on a span makes sense. – Peter M. - stands for Monica Nov 15 '17 at 15:47
  • I adjusted the XPath to //ul[@class='dropdown-menu inner selectpicker']/li/a and it still doesn't work. The click to element is skipped and no error message is produced. – Thabiso Ditabe Nov 15 '17 at 19:00
  • I think the HTML has no unique identifiers and it doesn't make it easy for the drive to locate elements on the dropdown. I have edited the question, please see the HTML. – Thabiso Ditabe Nov 15 '17 at 19:04
  • @VanderLinden I have added the correct HTML – Thabiso Ditabe Nov 16 '17 at 8:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.