By Arun Motoori
getFirstSelectedOption Selenium WebDriver command is used to retrieve the label text of the option that is first selected in the Multi-Selection Box field.
In order to use the getFirstSelectedOption( ) command with Multi-Selection Box field, we have to create an object for Select class of selenium for Multi-Selection Box field as shown below:Select select = new Select(_driver.findElement(By.cssPath(“CSS Path of Multi-Selection Box Field”)));
Here Select is the Class name
select is the object of Select Class
After creating an object for Select Class, you can get the getFirstSelectedOption( ) predefined method of Select Class by using its object select as shown below:select.getFirstSelectedOption( );
But the above statement will return the web element (i.e. retrieved option from the Multi-Selection Box field).
As our requirement is to retrieve the label text of the element that is first selected, we have to use getText( ) command to retrieve the label text of the retrieved Web Element by modifying the above statement as shown below:
select.getFirstSelectedOption( ).getText( ); -> This will retrieve the label text of the first selected option.
Follow the below steps to understand the test description:
1. Open http://compendiumdev.co.uk/selenium/basic_html_form.html2. Deselect all the options that are selected in Multi-Selection Box field using deselectAll( )
3. Select the option ‘Selection Item 2’ using selectByVisibleText( )4. Next select another option ‘Selection Item 3’ using selectByVisibleText( )5. So the first option selected by selectByVisibleText( ) is ‘Selection Item 2’ and the next option selected is ‘Selection Item 3’ as shown below:
6. If we use getFirstSelectedOption( ) command with getText( ) command in this case, it will retrieve the label text of the first selected option i.e. ‘Selection Item 2’
7. Now lets use getFirstSelectedOption( ) method to retrieve the label text of the first selected option from the Multi-Selection Box field as explained in the below screenshot:
Lets Implement the Test on Eclipse IDE:Pre-requisites:1. Create a new Java Project say ‘WebDriver-Project64’ in Eclipse IDE2. Configure the Project to work with Selenium WebDriver3. Create a package say ‘package64’ under the newly created project.4. Create a Java Class file say ‘Class64’ under the newly created package as shown below:
Actual Steps:1. Write the following code into the newly created Java Class file as shown below and make sure that you resolve all the errors before going to next step:
2. Create a test method ‘getFirstSelectedOptionDemo()’ which uses CSS Path Statement for locating the Multi-Selection Box field as shown below:
3. Save and Run the ‘Class64.java’ file by selecting the ‘JUnit Test’ option and ensure that our Automation Test has retrieved the label text of the first selected option ‘Selection Item 2’ from the Multi-Selection Box field and also entered the retrieved label text into a Username text box field as shown in the below video:Watch the below video:Click here to watch the video.Download this Project:
Click here to download this project and import into Eclipse IDE on your machine.Please comment below to feedback or ask questions.
Using isMultiple( ) with a drop down field will be explained in the next post.