0% found this document useful (0 votes)
64 views8 pages

8.list Box

The document describes various methods used to interact with list boxes in Selenium such as selectByIndex(), selectByVisibleText(), selectByValue(), deselect options, get the first selected option, check if a list box is multiple select, and get all options from a list box. Code examples are provided to demonstrate each method.

Uploaded by

Nandu Tata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views8 pages

8.list Box

The document describes various methods used to interact with list boxes in Selenium such as selectByIndex(), selectByVisibleText(), selectByValue(), deselect options, get the first selected option, check if a list box is multiple select, and get all options from a list box. Code examples are provided to demonstrate each method.

Uploaded by

Nandu Tata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

List Box :

List box is set of options

1)WebElement a = driver.findElement(By.xpath("xpathexp"));
2)Select s = new Select(a);

Folllowing methods are used for select the option from listbox

1.s.selectByIndex();
2.s.selectByVisibleText();
3. s.selectByValue();
----------------------------------
1.s.selectByIndex();

Example:

package ListBox;

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 ListBoxDemo {

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver",
"E:/Soft/a/chromedriver.exe");

WebDriver driver =new ChromeDriver();

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

driver.get("https://techcanvass.com/Examples/multi-select.html");
WebElement a =
driver.findElement(By.xpath("/html/body/div/form/fieldset/select"));

Select s = new Select(a);

s.selectByIndex(3);

----------------------------------------------------------------------------------------------------------------
---

2.s.selectByVisibleText():

package ListBox;

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 ListBoxDemo {

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver",
"E:/Soft/a/chromedriver.exe");

WebDriver driver =new ChromeDriver();

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

driver.get("https://techcanvass.com/Examples/multi-select.html");

WebElement a =
driver.findElement(By.xpath("/html/body/div/form/fieldset/select"));

Select s = new Select(a);

s.selectByVisibleText("Hyundai");
}

----------------------------------------------------------------------------------------
3. s.selectByValue():

Program:
package ListBox;

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 ListBoxDemo {

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver",
"E:/Soft/a/chromedriver.exe");

WebDriver driver =new ChromeDriver();

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

driver.get("https://techcanvass.com/Examples/multi-select.html");

WebElement a =
driver.findElement(By.xpath("/html/body/div/form/fieldset/select"));

Select s = new Select(a);

s.selectByValue("honda");
}

------------------------------------------------------------------------------------------------
Following methods are used for deselect the option from listbox

1.s.deselectByIndex();
2.s.deselectByVisibleText();
3. s.deselectByValue();
4. s.deselectAll();

---------------------------------------------------
getFirstSelectedOption()

Syntax:
WebElement a = driver.findElement(By.xpath("xpathexp"));
Select s = new Select(a);
s.selectByIndex(0)
WebElement a = s.getFirstSelectedOption();
System.out.println(a.getText());

Program:
package ListBox;

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 ListBoxDemo {

public static void main(String[] args) throws InterruptedException {


System.setProperty("webdriver.chrome.driver",
"E:/Soft/a/chromedriver.exe");
WebDriver driver =new ChromeDriver();

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

driver.get("https://techcanvass.com/Examples/multi-select.html");

WebElement a =
driver.findElement(By.xpath("/html/body/div/form/fieldset/select"));

Select s = new Select(a);

s.selectByIndex(0);//volvo

WebElement b = s.getFirstSelectedOption();
String c = b.getText();
System.out.println(c);

----------------------------------------------------------------------------------------------------------------
-----------------------

isMultiple()

Ex:
WebElement a = driver.findElement(By.name("States"));

Select s = new Select(a);


s.selectByIndex(0);
s.selectByIndex(1);
s.selectByIndex(2);

Boolean sm = s.isMultiple();
System.out.println(sm);

Program:

package ListBox;

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 ListBoxDemo {

public static void main(String[] args) throws InterruptedException {


System.setProperty("webdriver.chrome.driver",
"E:/Soft/a/chromedriver.exe");

WebDriver driver =new ChromeDriver();

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

driver.get("https://techcanvass.com/Examples/multi-select.html");

WebElement a =
driver.findElement(By.xpath("/html/body/div/form/fieldset/select"));

Select s = new Select(a);

s.selectByIndex(0);
s.selectByIndex(1);
s.selectByIndex(2);

Boolean sm = s.isMultiple();
System.out.println(sm);
}

---------------------------------------------------------------------------------
getOptions()
Program:
package ListBox;

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 ListBoxDemo {

public static void main(String[] args) throws InterruptedException {


System.setProperty("webdriver.chrome.driver",
"E:/Soft/a/chromedriver.exe");

WebDriver driver =new ChromeDriver();

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

driver.get("https://techcanvass.com/Examples/multi-select.html");

WebElement a =
driver.findElement(By.xpath("/html/body/div/form/fieldset/select"));

Select s = new Select(a);

List<WebElement> b = s.getOptions();
int c =b.size();//6
System.out.println(c);

for(int i=0;i<=c-1;i++) {//0,1,2,3,4,5,


String d =b.get(i).getText();
System.out.println(d);//volvo
Thread.sleep(2000);

driver.close();

===================

You might also like