Working with WebList in Selenium

In the previous post, we have seen different types of wait commands in Selenium. In this post, we will see how to work with WebList in selenium.

If you are automation any web application, you might have to handle WebList or say drop downs in some of the scenarios. In WebList, You can perform actions like select, deselect , randomly select and many more on the values available in to that. We will see how to perform these action.

Important note: We have create a demo application for working with elements like weblist,checkboxes,radio button ,links etc. Click Demo Application to access it.

It will look like as shown below.

DemoApplication for selenium


 

Let’s understand different operation on weblist with examples.

For performing operations on WebList, we use the Select class in selenium. We will look into some useful methods.

  1. selectByVisibleText()
  2. selectByIndex()
  3. selectByValue()
  4. deselectByVisibleText()
  5. getOptions()

Example of selectByVisibleText()This method select the value in  drop down by using the visible text. In our application, we have one drop down where we can select the numbers. In the code below, we are selecting number 4.

Weblist using selenium

Code for selecting the number 4:

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

	public static void main(String[] args) {

		WebDriver driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		WebElement source = driver.findElement(By.xpath("//select[@name='number']"));
		
		//use select class to work on the weblist items
		Select list = new Select(source);
		list.selectByVisibleText("4");   //select number 4

	}
}
Output: No. 4 is selected in drop down.

 

Example of  selectByIndex()This is used to select the item on the basis of index. Index starts from 0. If there are 5 values in the drop down, they will be accessible at the index 0,1,2,3,4 respectively. 

In our code , we have given the index 1. Index 0 has value ‘Select number’, index 1 has value 1, index value 2 has value 2 and so on.

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 WebListExample {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		 driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		WebElement source = driver.findElement(By.xpath("//select[@name='number']"));
		
		//use select class to work on the weblist items
		Select list = new Select(source);
		list.selectByIndex(2);   //select number 2				
		}
}
Output: No. 2 is selected in drop down.

We have one more type of list in the application where we can select multiple values.The default value selected in this is ‘Wednesday’.

SelectByIndex in selenium webdriver

If we select index 0 , then let’s see the result.

public class WebListExample {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		 driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		WebElement source = driver.findElement(By.xpath("//select[@name='Week']")); //multiple value list
		
		//use select class to work on the weblist items
		Select list = new Select(source);
		list.selectByIndex(0);   //select Monday 			
		}
}
Output: Monday is also selected in list with Wednesday which was by default selected.

SelectByIndex in selenium


Example of selectByValue(): It is one more way to select the list item on the basis of list values. You can check the list value with the help of Firebug.

SelectByValue in selenium

Suppose, we need to select the number 5, then its value is “five” as shown in the above screen.

public class WebListExample {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		 driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		WebElement source = driver.findElement(By.xpath("//select[@name='number']")); //multiple value list
		
		//use select class to work on the weblist items
		Select list = new Select(source);
		list.selectByValue("five");   //select 5 		
		}
}
Output: Number 5 will be selected.

Example of deselectByVisibleText()In the multiselect list, we can see ‘Wednesday’ is by default selected. What if we want to deselect it? It can be possible by using method deselectByVisibleText().

public class WebListExample {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		 driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		WebElement source = driver.findElement(By.xpath("//select[@name='Week']")); //multiple value list
		
		//use select class to work on the weblist items
		Select list = new Select(source);
		list.deselectByVisibleText("Wednesday"); //deselect Wednesday			
		}
}
Output: It will deselect Wednesday.

deselectByValue in selenium Webdriver

 

Similar way , we have more methods for deselecting.

  • deselectAll(): It will deselect all the selected values in the list.
  • deselectByIndex(index) : It will deselect on the basis of index as we have done with selectByIndex.
  • deselectByValue(String value): It will deselect the list item on the basis of Value of the list.

How to read all values of a webList?

We can read all the values in a weblist by using method getOptions(). This method will return the List of webelements and then in a for loop we can read the text property of all the webelements.

public class WebListExample {
	private static WebDriver driver;
	public static void main(String[] args) throws InterruptedException {
		
		 driver = new FirefoxDriver();
		
		driver.manage().window().maximize();
		driver.get("https://testingpool.com/wp-content/uploads/2015/08/DemoApplication.html");
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		WebElement source = driver.findElement(By.xpath("//select[@name='Week']")); //multiple value list
		
		//use select class to work on the weblist items
		Select list = new Select(source);
		List<WebElement> elements = list.getOptions();
		for(WebElement item : elements){
			System.out.println("List Item : "+item.getText()); //
		}
		}
}
List Item : Monday
List Item : Tuesday
List Item : Wednesday
List Item : Thursday
List Item : Friday
List Item : Saturday
List Item : Sunday

Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

3 Responses

  1. Balakrishna says:

    Good Work Guys….For Beginners your blog is very useful

  1. August 17, 2015

    […] the previous post, we have seen the working with WebList in selenium. In this post, we will see how to work on the check boxes and radio […]