Rayrun
โ† Back to Discord Forum

How to fill a text input without emptying its content?

julienmonnardposted in #help-playwright
Open in Discord
julienmonnard
julienmonnard

Hello there! I am trying to fill a text input, using the locator.fill() method, but without emptying its content first, I only want to add more text after the existing content. It seems the function automatically clears the content first, and I don't see any option to prevent this.

Is there any way to achieve this behaviour? Thank you for your help!

This thread is trying to answer question "No question was provided in the thread."

14 replies
julienmonnard
julienmonnard

Unfortunately it seems to behave the same. The only way I managed to make it work is by combining it with locator.press() So basically doing this:

const inputField = page.getByRole('textbox');
await inputField.press('ArrowRight'); // I guess this cancels the "selectAll" applied on the content?
await inputField.pressSequentially(' change');

Any chance an option like fill(' change', { clear: false }) would be possible to add? I feel like it would be a simpler api ๐Ÿ™‚

looks like a great feature request!

julienmonnard
julienmonnard

Cool! How do we handle feature requests in this project? Is it enough to mention it here, or should it be an issue or a discussion on Github?

julienmonnard
julienmonnard

Ah, found it, there is an issue template on Github ๐Ÿ‘

Thought this is where you need to use press..? SetFocus press('end') then send your new characters, has worked fine for me in the past with no issues, and should be the press off locator... if not they you are asking for issues...?

julienmonnard
julienmonnard

Yes, it does work, it's basically the option I show above. The downside is that you have to use pressSequentially instead of fill, which is less performant. And the docs say to use pressSequentially only for "special keyboard handling", which is not the case here. I tried to open a feature request, but it was not really received as I hoped... https://github.com/microsoft/playwright/issues/27484 ๐Ÿคทโ€โ™‚๏ธ This is not blocking me, I was just hoping for a more efficient approach to do this ๐Ÿ™‚

Might say the docs are not clear, or you're reading it the confusion the docs provide. If fill doesn't work, then i'd assert you are doing "special keyboard handling" simply for the reason fill() doesn't work...

tphillips8117

Is there anything wrong with grabbing what's in the box and calling .fill() with what was already in there, plus what you want to put on the end?

tphillips8117

what's the use case for just adding to the end of the text box?

I think locator.type(str) is what you need

tphillips8117

locator.type() is deprecated now, and has been replaced with pressSequentially(), which was suggested already ๐Ÿ˜„

julienmonnard
julienmonnard

@d3333333 I was reading "special keyboard handling" as for example using combo keys, or things like that.I don't consider editing existing value as being special keyboard handling ๐Ÿคทโ€โ™‚๏ธ

julienmonnard
julienmonnard

@tphillips8117 The use case would be to just extend a field's existing value, instead of replacing it completely. For example, I might want to take a screenshot of a form, that has a big textarea. And I want to see how it looks like when the textarea has a couple more lines. Instead of having to retype everything, I would prefer to just add the necessary content.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].