I have a situation I'm trying to handle. I am using webdriver, C#. when I tried using the CSSSelector it is just reading the parameter as a string. Kindly look into it.This is the HTML.

<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-7">&nbsp;</span>***
<abbr class="select2-search-choice-close"></abbr>   
<span class="select2-arrow" role="presentation">
<b role="presentation"></b></span></a><label for="s2id_autogen7" class="select2-offscreen"></label>
<input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-7" id="s2id_autogen7">
<div class="select2-drop select2-display-none select2-with-searchbox">   
<div class="select2-search">       
<label for="s2id_autogen7_search" class="select2-offscreen"></label>       
<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-7" id="s2id_autogen7_search" placeholder="">   
<ul class="select2-results" role="listbox" id="select2-results-7">   </ul></div>

The element I'm trying to get the ID is a dropdown with this kind of ID: id="select2-chosen-7". 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 -

IWebElement DropDownPath = driver.FindElement(By.CssSelector("*[id^='select2-chosen-'][id*='select2-chosen-']"));
SelectElement DropDown = new SelectElement(DropDownPath);
  • where is dropdown html code? Jul 2 '15 at 10:41
  • The first body of code at the top. Thanks. Exactly where there is -----
    – ken4ward
    Jul 2 '15 at 10:54
  • Yes but here in your question where exactly that code is? Jul 2 '15 at 11:08
  • In the body of code at the top , the 3rd line.
    – ken4ward
    Jul 2 '15 at 11:15
  • ok but that is span with blank value.. Jul 2 '15 at 11:24