Playwright JAVA: How to get the VALUE of an input field

Viewed 208

Hello Playwright experts,

I have this input field

<input id="myName" placeholder="e.g. Max" maxlength="15" class="xyz"/>

I have tried this JAVA code:

page.locator("[placeholder=\"e.g. Max\"]").textContent();

It does not work :-( but I can fill

page.locator("[placeholder=\"e.g. Max\"]").fill("Loren"); // this works

Could you help and has an idea?

Thank you in advance.

Cheers Loren

3 Answers

textContext won't return a value of an input. The same will happen if you try that in the DevTools.
You can use inputValue instead.
e.g.

page.locator("#myName").inputValue();

In case you want to assert your value you can use the .hasValue assertion. You can read more about it from the playwright docs.

assertThat(page.locator("#myName")).hasValue("input-value");
Related