Handling Dropdown Elements (or)
List Box:
Dropdown is developed usually by 'select' and 'option' tags
<select name= "cars" id="carid">
<option>
Maruthi </option>
<option>
Audi </option>
<option
selected> BMW </option>
<option>
Mercedes </option>
<option>
Kia </option>
</select>
Common Actions on Dropdown Elements
- Select a value in the dropdown
- Get the selected value in the dropdown
- Verify the ‘cars’ dropdown contains a
given value (e.g., 'Audi', 'Kia')
- Get the dropdown values count
- Get all dropdown values (e.g., Audi,
Maruthi, Kia, etc.)
Note:
If the dropdown is developed
using the <select> tag, Selenium
provides a Select class to handle it.
This class provides various methods to select and retrieve values
// Select : is predefined class in Selenium
//
has some methods , can select values in dropdown
Methods in Select
Class:
- selectByVisibleText(String text)
- Selects an option by its visible text.
- selectByValue(String value)
- Selects an option by its value attribute.
- selectByIndex(int index) -
Selects an option by its index (position)
ex: DropdownBasics program
package WebelementsBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class DropDownBasics1 {
public static
void main(String[] args) throws InterruptedException {
//open
chrome browser
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//
open url
-file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html");
Thread.sleep(2000);
Thread.sleep(3000);
//
Select : is predefined class in Selenium
//
has some methods , can select values in dropdown
// 1.selectByVisibleText(java.lang.String
text)
// 2.selectByValue(java.lang.String
value)
// 3.selectByIndex(int index)
//
Select 'Audi' value in cars dropdown
//
create obj for 'Select'class
WebElement carsDropdownEle = driver.findElement(By.xpath("//select[@name='cars']"));
Select
sel = new Select(carsDropdownEle);
sel.selectByVisibleText("Audi");
Thread.sleep(2000);
//
Select 'Kia' value in dropdown
sel.selectByVisibleText("Kia");
Thread.sleep(3000);
// get selected value in cars dropdown
// gettext()
<a> ToolTip link </a>
// <option> Kia </option>
// String
selectedValFromDropdown = carsDropdownEle.getText(); // dont use
WebElement
firstSelectedOptionEle =
sel.getFirstSelectedOption();
///
<option> Kia
</option>
String selectedValFromDropdown = firstSelectedOptionEle.getText();
//or
// we
can write above above code in single line
selectedValFromDropdown = sel.getFirstSelectedOption().getText();
// Kia
System.out.println("selectedValFromDropdown="+selectedValFromDropdown);
//HW Select ' Mercedes ' value in cars dropdown
//
HW get selected value in cars dropdown
//
HW Select 'Maruthi' values in cars dropdown ?
//
HW get selected value in cars dropdown
//
check dropdown down values are Case sensitive. so we have to give exact visible
text from cars dropdown
//
select 'audi' pass invalid text
// sel.selectByVisibleText("audi");//
error
// Thread.sleep(2000);
// "
org.openqa.selenium.NoSuchElementException: Cannot locate option with text:
audi
//
audi and Audi both are different . We have to give 'Audi' only but not 'audi'
//HW selecting
'Honda' val is not exist in cars
dropdown
}
}
2.SelectByValue(
"value "):
package WebelementsBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class DropDownBasicsBySelectByValue {
public static
void main(String[] args) throws InterruptedException {
//open
chrome browser
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Thread.sleep(3000);
//
open url
-file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html");
//
Select : is predefined class in Selenium
//
has some methods , can select values in dropdown
// 1.selectByVisibleText(java.lang.String
text) --> done
// 2.selectByValue(java.lang.String
value) -->
// 3.selectByIndex(int index)
//
2nd way SelectByValue("
value")
//
Select 'Mercedes' by Mercedesval
WebElement
carsDropdownEle =
driver.findElement(By.name("cars"));
Select
sel = new Select(carsDropdownEle); // 1 PC
sel.selectByValue("maruthival");
Thread.sleep(4000);
//
Select 'Audi' by audi val
sel.selectByValue("audival");
// HW
Select 'Kia' in dropdown using
selectByValue()
// HW
Select 'BMW' in dropdown using selectByValue()
// Case
Sensitive -
//
pass invalid value -AudiVal
sel.selectByValue("AudiVal");
//
error we have to give exact value attribute i.e 'audival'
// org.openqa.selenium.NoSuchElementException:
Cannot locate option with value: AudiVal
//
AudiVal and 'audival' both are diff
}
}
3. SelectByIndex(
int index)
index no always starts with index no= 0
1st dropdown value --> 0
2nd dropdown value --> 1
3rd dropdown value --> 2
.........................
ex:DropdownBasicsUsingSelectBYIndexNo
package WebelementsBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class DropDownBasicsBySelectByIndexNo {
public static
void main(String[] args) throws InterruptedException {
//
TODO Auto-generated method stub
//open
chrome browser
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Thread.sleep(3000);
//
open url
-file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html");
//
Select : is predefined class in Selenium
//
has some methods , can select values in dropdown
// 1.selectByVisibleText(java.lang.String
text) --> done
// 2.selectByValue(java.lang.String
value) --> done
// 3.selectByIndex(int index)
//
select 1st val in dropdown using index no =0 --> select maruthi
WebElement
carsDropdownEle = driver.findElement(By.name("cars"));
Select sel = new Select(carsDropdownEle);
sel.selectByIndex(0);
Thread.sleep(4000);
//
select 2nd val in dropdown using index no =1
sel.selectByIndex(1);
// HW
select 3rd value, in dropdown using index no
// HW
select 4th value in dropdown using index no
// -
ve index no = 5
sel.selectByIndex(5);
//
error : we dont have any value at
indexno 5
//
org.openqa.selenium.NoSuchElementException: Cannot locate option with index: 5
}
}
3 ways to select values in dropdown:
Select - class
Methods:
--------
selectByVisibleText(" Visible text from dropdown");
selectByValue(" value
propery")
selectByIndex(0);
FAQ** How can you handle dropdown
(or) Listbox?
Get all Values
From Dropdown :
package WebelementsBasics;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class GetAllValuesFromDropdown {
public static
void main(String[] args) throws InterruptedException {
//
TODO Auto-generated method stub
//open
chrome browser
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Thread.sleep(3000);
//
open url
-file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html");
//
get count of 'car's dropdown vals using
by select/option
//
count option tags in select tag
List<WebElement>
allOptionsTags = driver.findElements(By.xpath("//select[@name='cars']/option"));
int
drodownValsCnt = allOptionsTags.size();
System.out.println("drodownValsCnt="
+drodownValsCnt);// 5
// dont prefer this one
// way-2 using getOptions()
WebElement
carsDropDownEle=
driver.findElement(By.name("cars"));
Select
sel = new Select(carsDropDownEle);
List<WebElement> allOptions = sel.getOptions();
//
String, Webelement , List<Webelement>
int
cnt = allOptions.size();
System.out.println("cnt="+cnt);//5
// get all dropdown values Maruthi, Swiftvdi
,Mercedes
// <option
value="maruthival">Maruthi</option>
// <option
value="audival">Audi</option>....
//
get first val in dropdown - ie. Maruthi
String
val1Fromdropdown =
allOptions.get(0).getText();
System.out.println("val1Fromdropdown="+val1Fromdropdown);
//
HW get 2nd value in dropdown
// //
HW get 3rd value in dropdown
// HW
get 6 th value in dropdown?
//
get all values from cars dropdown ?
System.out.println("all
vals from dropdown =");
// allOptions.get(0);
// allOptions.get(1);
//
allOptions.get(4)
for(int
i=0;i<=cnt-1;i++)
{
String
val =allOptions.get(i).getText();
System.out.println("all
vals from dropdown ="+ val);
}
// all vals from dropdown =Maruthi
// all
vals from dropdown =Audi
// all
vals from dropdown =Kia
// all
vals from dropdown =BMW
// get all values from cars dropdown using "for each" loop?
for(WebElement eachOption: allOptions)
{
String
val = eachOption.getText();
System.out.println("get
all vals from dropown using for each loop="+ val);
}
// HW
3 get all values from cars dropdown
using iterator()?
//
HW 4. get all values from cars
dropdown using listIterator () ?
}
}
------------------------------------------------------
// HW Repeat all below things for 'bikes' dropdown?
// HW Select value in
'bikes' dropdown?
// HW get dropdown values count
in 'bikes' drop down?
// HW get selected value in 'bikes' dropdown?
// HW get all values from 'bikes' dropdown ?
--------------------------------------------
//FAQ : HW Select all values in dropdown one by one sequentially?
use for loop
sel.selectValue(),
Sleect ByVisisbleText(), SelectbyIndexno()?
2. sendkeys () for dropdown :
---------------------------
Dropdown Basics Using SendKeys :
package WebelementsBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DropDownBasicsUsingSendkeys {
public static
void main(String[] args) throws InterruptedException {
//open
chrome browser
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Thread.sleep(3000);
//
open url -file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html");
//
Send keys 'Audi'
driver.findElement(By.name("cars")).sendKeys("Audi");
Thread.sleep(3000);
//
Send keys 'BMW'
driver.findElement(By.name("cars")).sendKeys("BMW");
Thread.sleep(3000);
//HW Send keys 'Kia'
}
}
// HW Select Honda, yamaha
in 'bikes' dropdown using sendkeys()?
3. Handling
dropdown using click:
if dropdown is not developed by select tag, we have to use click
package WebelementsBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class DropdownUsingClick {
public static
void main(String[] args) throws InterruptedException {
//open
chrome browser
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Thread.sleep(3000);
//
open url -file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/SampleWebpage.html");
//
click on "cars" dropdown and click 'audival' specific value in
dropdown
driver.findElement(By.name("cars")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//option[text()='Audi']")).click();
Thread.sleep(3000);
//click
'bmwval'
driver.findElement(By.xpath("//option[text()='BMW']")).click();
//
click cars drodown ele and click Marithi
using text
//<option>Maruthi</option>
// //*[text()='Maruthi'] // use can this also
driver.findElement(By.xpath("//*[text()='Maruthi']")).click();
// HW
click "Mercedes" in cars
dropdown
// HW
click "Kia" in cars dropdown
}
}
// HW select values TVS,
yamha in 'bikes' dropdown using click , click()
Note:
· All
dropdowns are not developed by using 'select' tags in the project.
· Some
dropdowns are developed by different tags like div, ul, li, span tags.
<div>
<ul>
<li>
<span>
No comments:
Post a Comment