How to select a value from the drop-down list (Selenium C #)

advertisements

I need an assistance, how can I select a value from a drop down list, I am programming on C# with Selenium, and I am always getting the following error:

Element should have been select but was span

My code is:

internal static void SelectDD()
{
    SelectElement xxx= new SelectElement(driver.FindElement(By.XPath("/html/body/div[1]/section/div[1]/div[2]/section/div/section/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/div/div/a/span[1]")));

    xxx.SelectByIndex(1);
}

HTML:

<span class="select2-hidden-accessible" role="status" aria-live="polite"></span> <span class="select2-hidden-accessible" role="status" aria-live="polite"></span> <span class="select2-hidden-accessible" role="status" aria-live="polite"></span>
<div id="select2-drop-mask" class="select2-drop-mask" style="display: block;"></div>
<div id="select2-drop" class="select2-drop select2-display-none select2-drop-auto-width select2-drop-active select2-drop-above" style="left: 361.583px; top: 472.1px; bottom: auto; display: block; width: 262px;"></div>
</body>


First of all SelectElement can only be used with the element with Select tag. This is a div and SelectElement class does not apply here.

Try finding the element and just performing a click as follows:

IWebElement element = driver.FindElement(By.Id("select2-drop"));

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].click();", element);