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"> </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");
}
}