Search code examples
javascriptnode.jspuppeteerautofillwebautomation

How to find document.activeElement in Puppeteer


i want to autofill a form with puppeteer. I fill out the first input, then click on a button that then creates a new input field that has focus.

How can i select this input? Can i use document.activeElement and how?

  let newActivity = 'button.new_activity'
  await page.waitForSelector(newActivity)
  await page.click(newActivity)

// find active/focused input
await page.type(focusedInput, 'message')


Solution

  • You can use evaluateHandle to get the element handle, and then call the type function on that element.

    const el = await page.evaluateHandle(() => document.activeElement);
    await el.type('message');