WebDriver supports class called “SelectElement” in C#, which provides useful methods for interacting with Drop-Down. User can perform op...

Selenium - Select Method- How to Select Values from Drop Down Using Select Methods C#.


WebDriver supports class called “SelectElement” in C#, which provides useful methods for interacting with Drop-Down. User can perform operations to select a value from drop-down or even de-select the value from drop-down using it.

IWebDriver Driver = new ChromeDriver();

IWebElement CountryDropDownElement = Driver.FindElement(By.Name("India"));

SelectElement SelectCountry = new SelectElement(CountryDropDownElement);

SelectCountry.SelectByText("India");//There are 3 items - India, America, Australia

SelectCountry.SelectByIndex(0);//Index starts from 0. 0 = India, 1 = America

SelectCountry.SelectByValue("India");//There are 3 values - India, America, Australia


Difference between SelectByValue, SelectByText  and SelectByIndex

SelectByText 

Select all options by the text displayed.

It is nothing but selecting using actual values that are displayed to the user in drop-down, when they click on it for selecting
SelectCountry.SelectByText("Arab") –
<select name="Countries"><option selected> Please select</option>
  <option value="arab">Arab</option>
  <option value="america">America</option>
  <option value="test">India</option>
 </select>

This will select Arab in Drop-Down, which is again displayed to end-user in drop-down, when he clicks drop-down.

SelectByValue 

Select an option by the value.

We will be selecting the actual value from drop-down based on attribute ‘value’ (i.e. HTML).
SelectCountry.SelectByValue("test”) –

The above command will select the Value as “India” in the drop-down, because the html attribute value passed is “test” – You could see in below HTML –4th line the option value =”test” and Actual Drop-Down value is “India”.

Refer below HTML
<select name="Countries"><option selected> Please select</option>
  <option value="arab">Arab</option>
  <option value="america">America</option>
  <option value="test">India</option>
 </select>

SelectByIndex 

Selects the option by the index, as determined by the "index" attribute of the element.

This will select the value in drop-down based on their index, that is in the below html
<select name="Countries"><option selected> Please select</option>
  <option value="arab">Arab</option>
  <option value="america">America</option>
  <option value="test">India</option>
 </select>

Arab is at Index Number 0(first option value in above html), America at Index Number 1 , India at Index Number 3 and it will continue, if more value exists. So the values that are displayed first is always 0(index value) by default and it increments thereafter.

SelectCountry.SelectByIndex(0);
This will select the value as “Arab” in the drop-down.


Other Methods

SelectCountry.DeselectAll();

Clear all selected entries. This is only valid when the SELECT supports multiple selections.
Exceptions:
T:OpenQA.Selenium.WebDriverException:
Thrown when attempting to deselect all options from a SELECT that does not support multiple selections.

SelectCountry.DeselectByIndex(0);

Deselect the option by the index, as determined by the "index" attribute of the element.
Parameters: index:
The value of the index attribute of the option to deselect.
Exceptions:
T:System.InvalidOperationException:
Thrown when attempting to deselect option from a SELECT that does not support multiple selections.

T:OpenQA.Selenium.NoSuchElementException:
Thrown when no element exists with the specified index attribute.

SelectCountry.DeselectByText("India");

Deselect the option by the text displayed.
Parameters: text:
The text of the option to be deselected.
Exceptions:
T:System.InvalidOperationException:
Thrown when attempting to deselect option from a SELECT that does not support multiple selections.

T:OpenQA.Selenium.NoSuchElementException:
Thrown when no element exists with the specified test attribute.

Remarks:
When given "Bar" this method would deselect an option like:
<option value = "foo" > Bar </ option >
           
SelectCountry.DeselectByValue("test");

Deselect the option having value matching the specified text.
Parameters:value:
The value of the option to deselect.
Exceptions:
T:System.InvalidOperationException:
Thrown when attempting to deselect option from a SELECT that does not support multiple selections.
T:OpenQA.Selenium.NoSuchElementException:
Thrown when no element exists with the specified value attribute.

Remarks:
When given "foo" this method will deselect an option like:
<option value = "foo" > Bar </ option >

0 comments: