Search code examples
playwright-java

How to read the selected value of a dropbox using Playwright Java


Could someone give me an example how to read the selected value/option from a dropdown?

I have tried this syntax (JAVA), but it returns all the options of the dropdown.

page.locator("//xpath to dropdown webelement").textContent()

I only want to read what is selected.

Could you give an example how to do this with Java Playwright?


Solution

  • Give this a try:

    String searchValue = (String) page.evalOnSelector("//xpath to dropdown webelement", "el => el.value");
    

    Source