07. Select class

07. Select class

Handling the list box

  • A box on the screen that contains a list of options, only one of which can be selected or we can select multiple options.

?Example:


  • In selenium, the web element interface doesn’t have any appropriate method to handle the list box or drop downs.
  • In order to perform the actions on the drop down/list box, Selenium provides the class called ‘SELECT CLASS’.
  • WebDriver support classes called “Select”, which provides useful methods for interacting with select options.?
  • Select is a class which is provided by Selenium to perform multiple operations on Drop Down object and Multiple Select object.
  • This class can be found under the Selenium’s Support.UI.Select package.
  • It contains the following methods to perform the actions on the elements present in the drop down.
  • All the methods of select class are non-static, So in order to use the select class methods we need to create the object of select class.

  1. Select By Index
  2. Select By Visible Text
  3. Select By Value
  4. De-Select By Index
  5. De-Select By Visible Text
  6. De-Select By Value
  7. De-Select All
  8. Get All Selected Options
  9. Get First Selected Options
  10. Is Multiple
  11. Get Options

  • Following are the steps to use the select class.

Step 1: First identify the single drop down web element in UI.

Example: WebElement dropdown = driver.findElement(By.id(“XYZ”));

Step 2: Create the object of select class?

Example: Select sel = new Select();

Step 3: Pass the identified drop down web element to Select class constructor as an argument.

Example: Select sel = new Select(dropdown);

Step 4: Than handle the drop down using the methods available in select class.

Example: sel.SelectByIndex(1);??

Following is the sample HTML code to develop the drop down in the UI.

<Html>
    <Head>
 	<Title>Select Example by Index value</title>
    </Head>
	<Body>
	    <select name="Mobiles"><option value="0" selected> Please select</option>
		<option value="1">iPhone</option>
  		<option value="2">Nokia</option>
		<option value="3">Samsung</option>
		<option value="4">HTC</option>
		<option value="5">BlackBerry</option>
 	    </select>
	</body>
</html>        

Select the option by visible text:

SelectByVisibleText

SelectByVisibleText (String arg0): void?

  • It is very easy to choose or select an option given under any dropdowns and multiple selection boxes with selectByVisibleText method. It takes a parameter of String which is?one of the Value of?Select elements and it returns nothing.

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 DropDown 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("https://airindia.com/");
		// Finding the single select drop down in UI.
		WebElement ele = driver.findElement(By.id("ContentPlaceHolder1_UserLanguage1_drpCountry"));
		ele.click();
		// Creating the object of select class and passing the identified web element as an argument
		Select sel = new Select(ele);
		// Performing the action on drop down using the select class methods
		// Here we are selecting an element from a drop down using the visible text
		sel.selectByVisibleText("Australia");
		Thread.sleep(3000);
		driver.close();
	}
}        

Write a script to select the options from the drop down/list box Using select by index method?

Select By Index:

  • It is almost the same as selectByVisibleText but the only difference here is that we provide the index number of the option here rather the option text. It takes a parameter of int which is?the index value of?select element and it returns nothing.

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 DropDown 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("https://airindia.com/");
		// Finding the single select drop down in UI.
		WebElement ele = driver.findElement(By.id("ContentPlaceHolder1_UserLanguage1_drpCountry"));
		ele.click();
		// Creating the object of select class and passing the identified web element as an argument
		Select sel = new Select(ele);
		// Performing the action on drop down using the select class methods
		// Here we are selecting an element from a drop down using the visible text
		sel.selectByVisibleText("Australia");
		Thread.sleep(3000);
		driver.close();
	}
}        

Select By Value

  • It is again the same what we have discussed earlier; the only difference in this is that it asks for the?value of the option rather the option text or index. It takes a parameter of?String which is?one of the values?of?Select element and it returns nothing.

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 DropDown 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("https://airindia.com/");
		// Finding the single select drop down in UI.
		WebElement ele = driver.findElement(By.id("ContentPlaceHolder1_UserLanguage1_drpCountry"));
		ele.click();
		// Creating the object of select class and passing the identified web element as an argument
		Select sel = new Select(ele);
		// Performing the action on drop down using the select class methods
		// Here we are selecting an element from a drop down using the visible text
		sel.selectByVisibleText("Australia");
		Thread.sleep(3000);
		driver.close();
	}
}        

  • In the previous Example programs we worked on the drop down which allow us to select only one option.
  • I e, it won’t allow us to select the multiple options from the drop down.
  • If we try to use the de-selected methods like deselect by index, deselect by visible text, deselect by value and deselect all. Than we will get the following exceptions.

Exception in thread "main" java.lang.UnsupportedOperationException: You may only deselect options of multi-select

  • Deselected methods will only work with the drop down which allows us to select the multiple options.

What is multi-select drop down or list box?

  • The drop down which allows selecting the multiple options from the drop down/list box.
  • Following the HTML code used to develop the multiple options.

Write a script to select the multiple options from the drop down?

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 MultiSelectDrop 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("file:///C:/Users/HP/Desktop/multiSelect.html");
		WebElement ele = driver.findElement(By.name("Mobdevices"));
		Select sel = new Select(ele);
		// Selecting the multiple options
		sel.selectByIndex(1);
		sel.selectByValue("4");
		sel.selectByVisibleText("BlackBerry");
		driver.close();
	}
}        

Write a script to de-select the selected options by Index?

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 DeSlectByIndex 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("file:///C:/Users/HP/Desktop/multiSelect.html");
		WebElement ele = driver.findElement(By.name("Mobdevices"));
		Select sel = new Select(ele);
		// Selecting the multiple options
		sel.selectByIndex(1);
		sel.selectByValue("4");
		sel.selectByVisibleText("BlackBerry");
		// De-selecting the option by index
		sel.deselectByIndex(1);
		driver.close();
	}
}        

Write a script to select the selected option by visible text?

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 DeSelectByVisibleText 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("file:///C:/Users/HP/Desktop/multiSelect.html");
		WebElement ele = driver.findElement(By.name("Mobdevices"));
		Select sel = new Select(ele);
		// Selecting the multiple options
		sel.selectByIndex(1);
		sel.selectByValue("4");
		sel.selectByVisibleText("BlackBerry");
		// De-selecting the option by visible text
		sel.deselectByVisibleText("BlackBerry");
		driver.close();
	}
}        

Write a script to select the selected option by value?

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 DeSelectByVisibleText 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("file:///C:/Users/HP/Desktop/multiSelect.html");
		WebElement ele = driver.findElement(By.name("Mobdevices"));
		Select sel = new Select(ele);
		// Selecting the multiple options
		sel.selectByIndex(1);
		sel.selectByValue("4");
		sel.selectByVisibleText("BlackBerry");
		// De-selecting the option by value
		sel.deselectByValue("4");
		driver.close();
	}
}        

Write a script to de-select all the options from the drop down?

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 DeSelectAll 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("file:///C:/Users/HP/Desktop/multiSelect.html");
		WebElement ele = driver.findElement(By.name("Mobdevices"));
		Select sel = new Select(ele);
		// Selecting the multiple options
		sel.selectByIndex(1);
		sel.selectByValue("4");
		sel.selectByVisibleText("BlackBerry");
		// De-selecting all the option by de-select all method 
		sel.deselectAll();
		driver.close();
	}
}        

Write a script to check the drop down is single selected list box or multi select list box?

  • To check the drop down is single selected or multi select list box we use a method called “isMultiple ()”.
  • Ismultiple is a method available in select class, which is used to check the drop down, is single select or multi select drop down.
  • Return type of Ismultiple method is Boolean, which returns true if the drop down is multi select list box, returns false if the drop down is single selection drop down.

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 IsMultiple 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("file:///C:/Users/HP/Desktop/multiSelect.html");
		WebElement ele = driver.findElement(By.name("Mobdevices"));
		Select sel = new Select(ele);
		if(sel.isMultiple())
		{
			System.out.println("True: The drop down is multi selection drop down.");
		}
		else
		{
			System.out.println("False: The drop down is not a multi selection drop down.");
		}
		driver.close();
	}
}        

Output: True: The drop down is multi selection drop down.

Write a script to print the value of first selected option from the select drop down?

  • To get the first selected option we use a method called, get first selected option method of select class.
  • Get first selected method gets the first selected option from the drop down; We can use this method on single and multi-selection drop down.
  • This method returns the single web element. In order to get the text of the web element, we use web element method get text.
  • Consider a situation, where you used this method with selecting an element from the drop down. Then we will get the following exception.
  • Exception in thread "main" org.openqa.selenium.NoSuchElementException: No options are selected
  • So, we should use this method after selecting the option.

Write a script to print the selected options in the drop down?

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 GetFirstSelectedOption 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("file:///C:/Users/HP/Desktop/multiSelect.html");
		WebElement ele = driver.findElement(By.name("Mobdevices"));
		Select sel = new Select(ele);
		sel.selectByIndex(1);
		sel.selectByValue("4");
		sel.selectByVisibleText("BlackBerry");
		// Getting the first selected option using get first selected option
		WebElement element = sel.getFirstSelectedOption();
		String str = element.getText();
		System.out.println("The First selected option is: "+str);
		driver.close();
	}
}        

Output: The First selected option is: iPhone

Write a program to print all the selected option from the multi-select drop down?

  • In order to get all the selected options from the drop down.
  • We use a method called getAllSelectedOption method of select class.
  • Which gets all the options which are selected?
  • GetAllSelectedOption returns the list of web element.
  • In order to print all the option from the list of web element, first we need to get the size of web element.
  • In order to find the list web element count, we use a method called ‘size ()’.
  • The size () method returns size of the list of web elements, return type of this method is int.
  • Than we can navigate to each web element in the list of web elements and get the text from the web element.
  • Following is the code to get all the selected option from the drop down and printing all the selected option.

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 GetAllSelectedOption 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("file:///C:/Users/HP/Desktop/multiSelect.html");
		WebElement ele = driver.findElement(By.name("Mobdevices"));
		Select sel = new Select(ele);
		sel.selectByIndex(1);
		sel.selectByValue("4");
		sel.selectByVisibleText("BlackBerry");
		// Getting the all selected option using get all selected option method
		List<WebElement> element = sel.getAllSelectedOptions();
		// Counting the size of selected option
		int Count = element.size();
		System.out.println("Total Selected Option in the drop down: "+Count);
		for(int i=0;i<Count;i++)
		{
			String str = element.get(i).getText();
			System.out.println("The selected option is: "+str);
		}
		driver.close();
	}}        

Output:

Total Selected Option in the drop down: 3

The selected option is: iPhone

The selected option is: HTC

The selected option is: BlackBerry

?Write a program to get all the options from the drop down list?

  • In order to get all the options from the drop down, select class provides a method called getOptions ().
  • getOptions () method is used to get all the options present in the drop down/list box.
  • This returns the collections of web element from the drop down.
  • In order to get text of all web elements, first we need to take a count of web elements and then by accessing the entire web element in list of web elements, we can get the text of all the web elements.
  • Following is the code to get all the options from the drop down.

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 GetOption 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("file:///C:/Users/HP/Desktop/multiSelect.html");
		WebElement ele = driver.findElement(By.name("Mobdevices"));
		Select sel = new Select(ele);
		// Getting the all options using get options method
		List<WebElement> element = sel.getOptions();
		// Counting the size of selected option
		int Count = element.size();
		System.out.println("Total Options in the drop down: "+Count);
		for(int i=0;i<Count;i++)
		{
			String str = element.get(i).getText();
			System.out.println("The selected option is: "+str);
		}
		driver.close();
	}
}        

Output:

Total Options in the drop down: 6

The selected option is: Please select

The selected option is: iPhone

The selected option is: Nokia

The selected option is: Samsung

The selected option is: HTC

The selected option is: BlackBerry

Write a code to select all the options and de-select all the options?

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 SelectAllAndDeselectAll 
{
	public static void main(String[] args) throws InterruptedException 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.navigate().to("file:///C:/Users/HP/Desktop/multiSelect.html");
		WebElement ele = driver.findElement(By.name("Mobdevices"));
		Select sel = new Select(ele);
		List<WebElement> element = sel.getOptions();
		int Count = element.size();
		for(int i=0;i<Count;i++)
		{
			sel.selectByIndex(i);
		}
		sel.deselectAll();
		driver.close();
	}
}        

Write a script to search for the Specified option present in the List box.

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 Specifiedoption 
{
	public static void main(String[] args)
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
		driver.get("file:///C:/Users/HP/Desktop/multiSelect.html");
		WebElement listbox = driver.findElement(By.name("Mobdevices"));
		String eval ="HTC";
		String msg ="not found" ;
		Select selectmsg = new Select(listbox);
		List<WebElement> listitem = selectmsg.getOptions() ;
		for(int i =0 ; i< listitem.size() ; i++) 
		{
			String avalue = listitem.get(i).getText();
			if(eval.equalsIgnoreCase(avalue))
			{
				msg ="Found @index :"+ i ;
				break ;
			}
		}
		System.out.println(msg);
		driver.close();
	}
}        

Output: Found @index: 4

What is dynamic select drop down?

  • The content of the drop down is changing dynamically is called as dynamic select drop down.

How to work with dynamic select drop down?

  • Before selecting the option from the dynamic drop down, we should get all the option available in dynamic select box using get option, which returns collection of web elements, using for loop check for the expected value and select the value if option is available in the drop down.

Write a program to handle the dynamic select class drop down.

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 DynamicList
{
	public static void main(String[] args) 
	{
		WebDriver driver = new FirefoxDriver() ;
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.get("https://www.plus2net.com/php_tutorial/ajax-dd3.php");
		WebElement listboxcountry = driver.findElement(By.id("s1")) ;
		Select clist = new Select(listboxcountry) ;
		clist.selectByVisibleText("IND") ;
		for(int i =0; i< clist.getOptions().size(); i++)
		{
			System.out.println(clist.getOptions().get(i).getText());
		}
		WebElement stlistbox = driver.findElement(By.name("state"));
		Select slist = new Select(stlistbox) ;
		System.out.println(slist.getOptions().size());
		for(int i =0 ;i < slist.getOptions().size(); i++)
		{
			System.out.println(slist.getOptions().get(i).getAttribute("value"));
		}
		slist.selectByVisibleText("Madhya Pradesh") ;
		driver.close();
	}
}        

Assignment:

1. Write a script to print all the selected options with multi selected list box in reverse order.

2. Write a script to select the last option available in the drop down

Important:

  • If the list box/drop down is developed using the HTML tag <Select> than we can use the select class of selenium and we can handle the drop down/list box. But if the drop down is developed without using the <select> HTML tag then we cannot handle the drop down using the select class.

Customized List box

  • If any list box which is developed without using Select HTML tag is called as customized list box.
  • We can’t handle the customized list box using Select class. If we try to use it, we get unexpected tag name Exception.
  • In order to handle the customized list box we use the send keys method and we will directly enter the value into the text box.
  • Following is the script to enter the text using send keys method

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CustListBoxUsingSendKeys
{
	public static void main(String[] args) 
	{
		WebDriver driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
		driver.get("https://www.yatra.com/");
		WebElement listbox1 = driver.findElement(By.id("BE_flight_origin_city"));
		listbox1.sendKeys("Bangalore");
		listbox1.sendKeys(Keys.ENTER);
		WebElement arlist = driver.findElement(By.id("BE_flight_arrival_city"));
		arlist.sendKeys("Hyderabad") ;
		arlist.sendKeys(Keys.ENTER);
		driver.close();
	}}         

Assignment:

  1. Write a script to get all the options present in the customized list box?

要查看或添加评论,请登录

Srinivas Prasad K T的更多文章

社区洞察

其他会员也浏览了