1

enter image description here

I have a situation I'm trying to handle. I am using webdriver, C#. The image above is the dropdown I want to select from. This is how it works, to select from the element, first click it and a text box to enter whatever you want to pick from the list appears. Once the search is made, then select the element. The code is below. The first body of code denotes when it has not been clicked.

<div class="select2-container select2" id="s2id_UserRole" style="width: 100%;">
    <a href="javascript:void(0)" class="select2-choice" tabindex="-1">   
        ***<span class="select2-chosen" id="select2-chosen-9">&nbsp;</span>***
            <abbr class="select2-search-choice-close"></abbr>   
            <span class="select2-arrow" role="presentation"><b role="presentation"></b></span>
        </a><label for="s2id_autogen9" class="select2-offscreen"></label>
    <input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-9" id="s2id_autogen9">
</div>

The element I'm trying to get the ID is a dropdown with this kind of ID: id="select2-chosen-9". The "select-chosen-" is static and numeric part is dynamic. After reading forums, all suggestion does not seem to have the answer. At the end this is the code I'm using which is still not working.

I got to understand that the search is a text which has this body of code. The code below depicts that.

<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" 
role="combobox" aria-expanded="true" aria-autocomplete="list" aria-owns="select2-results-3" id="s2id_autogen3_search" 
placeholder="" aria-activedescendant="select2-result-label-11">

This is the code I have tried but still not giving the right output:

IList<IWebElement> DropDownPath = driver.FindElements(By.CssSelector("span[id^='select-chosen-']"));
for (int i = 0; i < DropDownPath.Count; ++i)
{
    if (DropDownPath.Count == 3)
    {
        new UserValidate(driver).WaitElementVisible(DropDownPath[2].ToString());
        driver.FindElement(By.CssSelector(DropDownPath[2].ToString())).Click();
        IWebElement ele = driver.FindElement(By.CssSelector("#s2id_autogen3_search"));
        SelectElement DropDown = new SelectElement(ele);
        DropDown.SelectByText("SuperAdimn");
    }
}
2
  • Can you provide the URL? Commented Jul 3, 2015 at 17:29
  • It's a secured site.
    – ken4ward
    Commented Jul 6, 2015 at 8:06

1 Answer 1

1

Your code logic seems okay but there are some parts which need to be changed.

  1. First when you want to search a non static value in dynamic id, searched value should match correct pattern so your pattern can not find expected span , this code line should be changed to:

    By.CssSelector("span[id^='-chosen-']"));
    

    Comment : I hope there is not any span which includes "-chosen-" in it.

  2. There is a spelling mistake, please changed last line to

    DropDown.SelectByText("SuperAdmin");
    

Your Answer

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

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