1

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();
    driver.findElement(By.xpath("//button[@data-id='idSbxPractice']")).click();
    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");

        if(innerhtml.contentEquals("OptionSelect"))
        {
            element.click();
            break;
        }
        System.out.println("values from dropdown is ======= "+innerhtml);
    }
        driver.quit();
}

HTML :

<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>
<div class="modal-body">
<div class="row">
<div>
<div class="col-md-12">
<div>
<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;"
<option>
<span> class="text" title="SelectOption">SelectOption</span>
</option>

closed as off-topic by c32hedge, Bharat Mane, IAmMilinPatel, alecxe Nov 20 '17 at 15:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers." – c32hedge, Bharat Mane, IAmMilinPatel, alecxe
If this question can be reworded to fit the rules in the help center, please edit the question.

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

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

Select _select = new Select(driver.findElement(By.cssSelector("#idSbxPractice")));
try {
    _select.selectByVisibleText("OptionSelect");
} 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.

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

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.

EDIT

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.

  • Clicking on a link instead on a span makes sense. – Peter M. 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.