I am trying get text of the selected option from the dropdown, but it is rather returning all the options available.
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 ?
The ‘value’ of the selected dropdown object should be text.
See the solution in Selenium.
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 . Thanks.
I have this HTML code and the first option is selected so I want the selected option text, not a value in katalon.
<option value="1" selected="selected">India</option>
my test object like this
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:
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!
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
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
It might help for this type of scenario