Handle Dropdowns and Date Fields

Dropdowns and Multiple select are handled through Select class available in webdriver. It is available under the Selenium’s Support.UI.Select package. It provides the implementation of the HTML SELECT tag . There are various methods expose by Select out of which webelements can be selected and unselected by  SelectBy methods and DeSelectBy methods.

Select select = new Select(<WebElement object>);

 
SelectBy methods
  1. selectByVisibleText Method.
  2. selectByIndex Method.
  3. selectByValue Method.
DeSelectBy methods
  1. deselectByIndex Method.
  2. deselectByValue Method.
  3. deselectByVisibleText Method.
  4. deselectAll Method.

 

Handle dropdown using webdriver commands Example

SelectBy Methods 

It works for single selection as well as multiple selection

Let’s select “Python or PHP” in the dropdown by using different SelectBy methods.  

 

1. SelectByVisibleText

Here dropdown value is selected by the visible text. It takes parameter as a string and returns nothing.

Command: <Obj>.selectByVisibleText(String);

Select select = new Select(driver.findElement(By.id("select")));
select.selectByVisibleText("Python");
select.selectByVisibleText("PHP");
 
2. SelectByIndex

Here dropdown value is selected by the index value. It takes integer as a parameter and returns nothing.

Command: <Obj>.selectByIndex(int);

Select select1 = new Select(driver.findElement(By.id("select")));
select1.selectByIndex(2);
 
3. SelectByValue

Here dropdown value is selected based on the value. It takes parameter as a string and returns nothing.

Command: <Obj>.selectByValue(String);

Select select2 = new Select(driver.findElement(By.id("select")));
select2.selectByValue("pp");

Practice yourself: You can try automating “Selects field by yourself.

 


DeSelectBy Methods 

This works only for multiple selection

Let’s select “Python & PHP” in the dropdown by using different SelectBy methods. 

 
1. DeselectByIndex Method

Deselects the option of a given index

Command: <Obj>.deselectByIndex(int);

Select deselect = new Select(driver.findElement(By.id("select")));
deselect.deselectByIndex(2);
 
2. DeselectByValue Method 

Deselects the option of a given value

Command: <Obj>.selectByValue(int);

Select deselect1 = new Select(driver.findElement(By.id("select")));
deselect1.deselectByValue("pp");
 
3. DeselectByVisibleText Method 

Deselects the option of a given Text

Command: <Obj>.selectByIndex(int);

Select deselect2 = new Select(driver.findElement(By.id("select")));
deselect2.deselectByVisibleText("PHP");
 
4. DeselectAll Method 

Deselects all the options

Command: <Obj>.selectByIndex(int);

Select deselect3 = new Select(driver.findElement(By.id("select")));
deselect3.deselectAll();




Get Methods

GetOptions

This will return all the options available. It takes nothing as a parameter and return List<WebElement>

Command: <Obj>.getOptions();

Select selectg = new Select(driver.findElement(By.id("select")));
selectg.getOptions();
 
GetAllSelectedOption

This will return all the selected option. It takes nothing as a parameter and return List<WebElement>

Command: <Obj>. getAllSelectedOptions();

Select selectgA = new Select(driver.findElement(By.id("select"))); 
selectgA.getAllSelectedOptions();

 


Date Fields

//Fill system date in “MM/dd/yyyy” format

DateFormat dateFormat = new SimpleDateFormat(“MM/dd/yyyy”);

//get current date time with Date()

Date date = new Date();

// Now format the date

String dateFormatted= dateFormat.format(date);

       

//Fill system datetime in “MM/dd/yyyy HH:mm:ss” format

DateFormat dateFormat = new SimpleDateFormat(“MM/dd/yyyy HH:mm:ss”);

//get current date time with Date()

Date date = new Date();

// Now format the date

String dateFormatted= dateFormat.format(date);

 

//Fill date as mm/dd/yyyy as 04/16/2020

dateBox.sendKeys(“04162020”);

Practice yourself: You can try automating “Date Picker field by yourself by using above examples