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:

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.

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.

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.

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().

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.

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
Print Friendly, PDF & Email
Different Types Of Wait in Selenium
Working with CheckBoxes and Radio Buttons
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

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 […]

Leave a Reply

Your email address will not be published. Required fields are marked *