Dotan
Dotan

Reputation: 7622

JS Puppeteer: enter multiple chars atomically

I'm trying to fill an html form with puppeteer using type. According to the docs:

page.type(selector, text[, options])

...

Sends a keydown, keypress/input, and keyup event for each character in the text.

I have an issue with other events interfering with my typing process. How can I type my text atomically? I.e. with a single keydown event?

Upvotes: 0

Views: 1065

Answers (2)

NoriSte
NoriSte

Reputation: 3709

I don't know if it fits your needs but if you want to avoid keyboard events you could set directly the input value

await page.evaluate(() => document.querySelector("YOUR_SELECTOR").value = "Puppeteer");

or, if you need just one keydown event, you can trick Puppeteer by setting the value to "almost" what you need

await page.evaluate(() => document.querySelector("YOUR_SELECTOR").value = "Puppetee");

and use type just for the last char

await page.type("YOUR_SELECTOR", "r");

Upvotes: 1

Joel
Joel

Reputation: 1140

you can do, page.keyboard.sendCharacter("text"), which will do something akin to pasting in the text all at once. Make sure to first focus the selector you want to type into.

await page.focus('selector');
await page.keyboard.sendCharacter('text');

Upvotes: 3

Related Questions