How to get text of the selected option in the dropdown

I am trying get text of the selected option from the dropdown, but it is rather returning all the options available.

String selectedOption=WebUI.getText(findTestObject(’…’))

You will probably want to use the getAttribute command with the ‘value’ syntax.

selectedOption = WebUI.getAttribute(findTestObject(‘dropdownObjectName’),‘value’)

Is there any way to get text of selected option rather than value ?

1 Like

The ‘value’ of the selected dropdown object should be text.

See the solution in Selenium.

import org.openqa.selenium.By

import com.kms.katalon.core.webui.driver.DriverFactory

Select select = new Select(DriverFactory.getWebDriver().findElement(By.xpath("//select[@id='myDropdown']")))
String optionLabel = select.getFirstSelectedOption().getText()

I believe ‘value’ refers to the content of the field, not specifically a numerical value. The content of the field should be returned as a String whether there is a number or text data.

If that’s not the case, what behavior are you seeing when using value for your scenario?

Can you provide the HTML of the select element and associated options? It’s usually easier to provide a solution if we have the whole picture :slight_smile:. Thanks.

I have this HTML code and the first option is selected so I want the selected option text, not a value in katalon.

<select id="ddl">
      <option value="1" selected="selected">India</option>
      <option value="2">US</option>
      <option value="2">UK</option>

my test object like this

findTestObject(Object Repository/CountrySelect)

this is done by using code
i don’t want to add any logical code, is there any built-in keyword for that

I don’t think so. At least I am not aware of any keyword for this.

I didnt try it myself so im not 100% sure if it’ll work. But maybe this helps

You should be able to get the text with built in keyword get text. In script view it should look like

selectedOptionText = WebUI.getText(findTestObject(‘xyz/your_object’))

To check against the text:

WebUI.verifyEqual(selectedOptionText , “India”)

Your object should have an xpath which looks like following to make sure your object is the one which is currently selected:

1 Like

The reason you are getting the text for ALL options under the select is that you are asking for the text of the ‘select’ element, which will grab ALL child text nodes beneath it (all of the texts of all of the ‘option’ elements). What you need is to get the text of ONLY the selected ‘option’ element. You do this either by creating a separate TestObject that locates the selected option (your xpath would look something like “//select[@id=’'ddl”]/option[@selected]"), or you do what Marek has suggested (and I would agree with).

I know that it’s convenient to try and stick with the WebUI methods already available to you, but you’re doing yourself a disservice by not utilizing the power of Custom Keywords.

Hope this helps!

1 Like

I recently had to revisit this topic and came up with this:

String allBranchLocations = WebUI.getText(findTestObject('Dropdown Location'))
List allBranchesList=allBranchLocations.split("\\r?\\n") //Remove CRLF from each dropdown entry
String branchName=allBranchesList[0]

This converts the String to a List based on a Split for the CRLF of each entry from the dropdown.
The currently selected value in the dropdown is index[0]
It might help for this type of scenario