0

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?

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
lorenlai
  • 51
  • 7

1 Answers1

0

Give this a try:

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

Source

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
AppleTree
  • 16
  • 2
  • 1
    @lorenlai If their answer was useful, please upvote it. If it solved your problem, please be sure to accept it as the answer by clicking the checkmark to the left of their answer. – Jeremy Caney Aug 04 '22 at 02:23
  • what needs to be done, when I have frames? usually I have this ... page.framLocator("#iframeContainer").locator("//select") ... I have tried with page.framLocator("#iframeContainer").evalOnSelector("//...", "el => el.value ") ... syntax is invalid ? :-( could you help again? and make an example? – lorenlai Aug 19 '22 at 13:53
  • You will probably use a 'frame.evalOnSelector' statement with frames. Source: https://playwright.dev/java/docs/api/class-frame#frame-eval-on-selector – AppleTree Aug 21 '22 at 21:28