07. Select class
Srinivas Prasad K T
Engineering Manager @ Decathlon Technology || Ex Harman || Ex E2Open || Oracle Java? || Spring Boot? || WDIO? || Playwright? || Java Script & Type Script? || Scrum? || Gatling? || AI? || Tricentis Tosca? || ACCELQ?
Handling the list box
?Example:
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?
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:
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
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();
}
}
Exception in thread "main" java.lang.UnsupportedOperationException: You may only deselect options of multi-select
What is multi-select drop down or list box?
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?
领英推荐
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?
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?
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?
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?
How to work with dynamic select 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:
Customized List box
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: