How print a selected item in drop-down

I swear I spent the last six hours on the forum, looking for the answer to something so simple.

How to capture the text that is appearing in the drop-down (selected)?

hi,

you can use two different methods to get text value from UI


or

@xupacabra If it is still in the drop-down state, and you need to do something with the text, then you need something like:

import org.openqa.selenium.support.ui.Select

Select select = new Select(DriverFactory.getWebDriver().findElement(By.xpath("pathway here")));

def selectedItem = select.getFirstSelectedOption().getText();

println('The item selected is ' + selectedItem)

OR if you just want to ensure it is correct:

WebUI.verifyOptionSelectedByLabel(findTestObject('to'), 'your Selected Text', false, 10)

thank you guys
I read everything and decided to take a test
I got it as follows:

import org.openqa.selenium.By
import org.openqa.selenium.support.ui.Select
import com.kms.katalon.core.webui.driver.DriverFactory

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

Source: How to get text of the selected option in the dropdown

That’s probably where I found the code. I just store all the tidbits in my wiki for later lookup (maybe I should keep the link too). What I wanted to also say is if you have the id of the element, then instead of using xpath, you can use another form of the statement:

findElement(By.id('myDropdown')))

1 Like

Hummm cool