Friday 30 June 2017

Selenium WebDriver : Working with Drop-down Options

So, by now you must have learnt about how to open a Web Page, Navigate to another page by clicking on a link, click a button or verify text present on the Web Page. If not, you need to practice these basic User Actions that can be performed through Automation. You can also check the videos which explains about the different things that we have already discussed and will be discussing in the upcoming posts, related to Test Automation using Selenium WebDriver.

 YouTube Channel : Click Here

In this post we are going to learn yet another important feature of Selenium WebDriver, that is Selecting one of the Drop-down Options.

Before we see how to use the methods provided by Selenium WebDriver to select Drop-down Options, we need to initialize the Object which we will be using to Select options. Following line of code will find the <select> tag in HTML source code, and assign it to an object which we can use to call different methods :



Select option = new Select(driver.findElement("<select> tag locator"));

Once we have initialized the Object, we are ready to use the Object to call different methods.


There are different ways of selecting Drop-down option, let's see them one by one :


1 - Select By Visible Text :

You can select options based on the values displayed in the drop-down list.


option.selectByVisibleText("Option text String");

This will select the option based on the String passed as parameter to above mentioned method.


2 - Select by Value :


option.selectByValue("Option value attribute");

You can find the value attribute of the <option> tag in the HTML source code.

  

3 - Select by Index :


option.selectByIndex(index);

index is 0-based (starts from zero), to be incremented by one for next option.



In this way we can Select any of the Drop-down options using any of the methods provided by Selenium WebDriver.

Feel free to comment in the comments section in case you have any queries.
Happy Learning.

Don't forget to Subscribe :
YouTube Channel : Click Here

No comments:

Post a Comment