Performing Drop Down Operations in Selenium

How do you drop down in Selenium?

Drop down elements are common in web pages, especially when a user is registering on a website and has to enter the country/city related information. In Selenium C#, drop down operations are performed using the SelectElement class.

SelectElement is declared in using OpenQA.Selenium.Support.UI package which is imported before performing any operation on the drop down box.

Drop down operations in Selenium C#:

1. SelectByIndex - Select a value based on the index value. The starting index in drop down is 0. Syntax: void SelectElement.SelectByIndex(int index)

2. DeselectByIndex - Deselect a selected value based on the index. Syntax: void SelectElement.DeselectByIndex(int index)

3. SelectByText - Select an option based on the text of the options available in the drop down. There is an option to do partial matching of the text and by default partial matching is False. Syntax: void SelectElement.SelectByText(string text, [bool partialMatch = false])

4. DeselectByText - Deselect an already selected option based on the text of the options available in the drop down. Syntax: void SelectElement.DeselectByText(string text)

5. SelectByValue - Select an option based on the value supplied as input. Syntax: void SelectElement.SelectByValue(string value)

6. DeselectByValue - Deselect an already selected option based on the value supplied as input. Syntax: void SelectElement.DeselectByValue(string value)

7. DeselectAll - Used to clear the selected options in multi select drop down menus. Syntax: void SelectElement.DeselectAll()

8. IsMultiple - This Selenium Webdriver command gets a value showing if the parent element supports multiple selections. If the drop down is multi select capable it returns True else otherwise it returns False. Syntax: bool SelectElement.IsMultiple{get;}

9. Options - You can get the list of options for the select element with this Selenium WebDriver command. Syntax: IList<IWebElement> SelectElement.Options{ get; }

10. AllSelectedOptions - Gets all of the selected options within the select element. It is similar to Options except that it should be used on a multi select drop down menu. Syntax: IList<IWebElement> AllSelectedOptions { get; }

8 Likes