SELECT DROP DOWNS

 

Working  with select dropdown to word with select html tag element select  single select combo box or multi select list box we need to use select class on the select html element can done the following     

1.  Select an optiono or select multiple option
2.  Deselect an option
3.  Get all available options
4.  Get selected options

1.  To select an option from single select combo box or to select multiple options from list box select class provides 3 different methods.

             1 select byindex()

             2 select byvalue()   

             3 select byvisibility

Select by an index an integer argument which is zero based index of the option to be selected by default each option tag will have index starting from zero

Select by the value takes string argument which is value of the attribute of the option tag

Select by the visible takes string argument which is text at option tag select

In multi select list box the same method will be used however, if we write select method multiple times in the code I t will select multiple options from the list box.


2.   Deselecting option from multi select list to deslect all of the optons we can use deselectAll()


To deselect individual options ue can use

  1. Deselect byindex()
  2. Deselect byvalue()
  3. Deselect visible() 
SINGLE SELECT DROP DOWN 

package  mypackage;


import java.util.concurrent.TimeUnit;

 

import  org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import  org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;


public class Singleselect {


public Static void main(String[] args)    {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://ssc.nic.in/registration/home");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 

WebElement singleselect=driver.findElement(By.id("TypeofID"));

Select ss=new Select(singleselect);

ss.selectByIndex(1);

ss.selectByValue("2");

ss.selectByVisibleText("Passport");


SHORTCUT

Select ss=new Select(driver.findElement(By.id("TypeofID")));

ss.selectByIndex(1);

ss.selectByValue("2");

ss.selectByVisibleText("Passport");

}

}


MULTI SELECT DROP DOWN

package mypackage;


import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import  org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;


public class Multiselect {


public Static void main(String[] args)   {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 

WebElement multiselect=driver.findElement(By.id("multi-select"));

Select ms=new Select(multiselect);

ms.selectByIndex(0);

ms.selectByVisibleText("New Jersey");

}

}

SHORTCUT

Select ms=new Select(driver.findElement(By.id("multi-select ")));

ms.selectByIndex(1);

ms.selectByVisibleText("New Jersey "); 


SELECTING AN OPTION

To deselect individual options we can use 

1 deselecting byindex();

2 deselecting by value();

3 deselecting byvisibleText();

Deselecting all of the options we can use deselecting All();


GET SELECT OPTIONS

           To get all the selected options from a multi select list box, we can use the method getAllSelectedOptions() which returns a list of web element (Collection of all the select options). We can use a for loop and get each selected option and print the text of each element.

 

package demopack;

 

import java.util.List;

import java.util.concurrent.TimeUnit;

 

import  org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;

 

public class DemoAlloptions {

 

public static void main(String[] args)  {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

 

WebElement multiselect=driver.findElement(By.id("multi-select"));

Select ms=new Select(multiselect);

ms.selectByIndex(0);

ms.selectByVisibleText("New Jersey");

 

List<WebElement> alloptions=ms.getAllSelectedOptions();

int si=alloptions.size();

System.out.println(si);

                   

for (int i = 0; i < sii++) {

WebElement option=alloptions.get(i);

String te=option.getText();

System.out.println(te);

}

}

}


GET ALL AVAILABE OPTIONS

          To get all the available options select class provides a method getoptions() which a collection of all the options in the select dropdown. This collection is represented as a list of webElement. Using a for loop we can iterate the list, get each element from the List print the text of each element (Text of the option tag or visible text).

Package mypackage;

 

import java.util.List;

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.support.ui.Select;

 

public class Alloptions {

 

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.get("https://ssc.nic.in/registration/home");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                   

WebElement singleselect=driver.findElement(By.id("TypeofID"));

Select ss=new Select(singleselect);

ss.selectByIndex(1);

ss.selectByValue("2");

ss.selectByVisibleText("Passport");

                   

List<WebElement>alloptions=ss.getOptions();

int si=alloptions.size();

System.out.println(si);

                   

for (int i = 0; i < sii++) {

WebElement option=alloptions.get(i);

String text=option.getText();

System.out.println(text);         

)        

}