Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill html textarea element with playwright-java?

I have a textarea html tag element and need to update the text within it. For example, here is what the tag looks like:

<textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px; resize: none; display: none;"></textarea>

Question: how can I add a string inside of the textarea html tag? For example, I would like to add "hello" in the html tag and make it look like this afterwards:

<textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px; resize: none; display: none;">hello</textarea>

I have tried this code:

page.fill(".g-recaptcha-response", "hello");

However, it throws this error: "waiting for selector .g-recaptcha-response. selector resolved to hidden <textarea id="g-recaptcharesponse" name="g-recaptcha-response...> elementHandle.fill("hello") waiting for element to be visible, enabled and editable, element is not visible - waiting...

like image 894
blizzardforce Avatar asked Mar 07 '26 07:03

blizzardforce


1 Answers

Playwright has something called Auto-waiting/Actionability by default.

https://playwright.dev/java/docs/actionability

In that case the fill method is waiting for the textarea to be visible, but since that element is always invisible, it throws an exception after some time.

You could try to set the force tag to true in order to ignore those actionabilities, but honestly it didn't work for me:

page.locator("#g-recaptcha-response").fill(solution, new Page.FillOptions().setForce(true));

What I ended up doing was executing a Javascript code through evaluate:

page.evaluate("solution => document.querySelector('#g-recaptcha-response').innerHTML = solution", "hello");

PS: I translated the code above from C# to Java, so I didn't tested it, but it'll probably work as well.

like image 188
guilherme.osaka Avatar answered Mar 08 '26 20:03

guilherme.osaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!