We can use the Selenium WebDriver method getAttribute("value") to retrieve the value of the list option that is currently selected in the drop down field.
How to find the values of the list options in Drop Down field manually ?
Follow the below steps to find out the values of Drop Down fields list options onhttp://compendiumdev.co.uk/selenium/basic_html_form.html page :
1. Open http://compendiumdev.co.uk/selenium/basic_html_form.html page
2. Right click on the page and select 'View Page Source' option as shown below:
3. Ensure that the HTML source code of the page is displayed and view the code for drop down field as shown below:
4. Identify the values of the list options in the Drop down field code as shown below:
So this is how we find the values of the Drop Down fields list options. Write down the value of the 'Drop Down Item 4' i.e. 'dd4' which we will be using in the coming example.
Test Description:
Now lets use getAttribute("value") method to retrieve the value of the selected list option as explained in the below screenshot:
Lets Implement the Test on Eclipse IDE:
Pre-requisites:
1. Create a new Java Project say 'WebDriver-Project62' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package62' under the newly created project.
4. Create a Java Class file say 'Class62' 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 'retrieveAndVerify()' as shown below:
3. Use Selenium WebDriver command getAttribute("value") method for retrieving the value of the current selected list option from the Dropdown field and also use JUnit method assertEquals( ) to verify whether the value of the list option that is retrieved using the getAttribute("value") method is equal to the value of the list option that is currently selected in the Dropdown field as shown below:
4. Save and Run the 'Class62.java' file by selecting the 'JUnit Test' option and ensure that our Automation Test has passed to confirm that the value of the list option retrieved using getAttribute("value") is equal to the value of the list option that is currently selected 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 getFirstSelectedOption( ) with drop down field to retrieve the label text of the list option that is currently selected will be explained in the next post.