How to handle clipboard operations with Puppeteer automation

I’m trying to automate clipboard functionality using Puppeteer but running into issues with the copy operation. Here’s my current approach:

const puppeteer = require('puppeteer');
const fs = require('fs');

(async () => {
    const browserInstance = await puppeteer.launch({ headless: false });
    const browserContext = await browserInstance.createBrowserContext();

    await browserContext.overridePermissions('', [
        'clipboard-read',
        'clipboard-write',
    ]);

    const webPage = await browserInstance.newPage();
    await webPage.goto('');

    const { width, height } = webPage.viewport();

    await webPage.mouse.click(width / 2, height / 2);

    await webPage.keyboard.down('Control');
    await webPage.keyboard.press('a');
    await webPage.keyboard.up('Control');

    const clipboardData = await webPage.evaluate(() => {
        return navigator.clipboard.readText();
    });

    fs.writeFileSync('output.txt', clipboardData);

    await browserInstance.close();
})();

My script sets up the clipboard permissions correctly and simulates the Ctrl+A key combination to select all content. The issue is that while I can paste content from my system clipboard into the file successfully, the actual copying part within the automation doesn’t seem to work as expected. The clipboard reading returns empty or incorrect data. What am I missing in my implementation?

The clipboard reading fails because you’re using goto('') with an empty string. Browser security blocks clipboard access on blank pages or pages without proper origins. I’ve hit this same issue - clipboard APIs work fine on real websites but break on empty or local pages.

Navigate to a real URL first, or serve your local content through an HTTP server. You could also launch Puppeteer with --disable-web-security, but don’t do that in production. The permission override will work once you’ve got a proper page with a valid origin.

Yeah, it’s probably a timing issue. Clipboard operations are async and your script doesn’t wait for the copy to finish before trying to read it. Try a longer delay or use page.waitForFunction() to check if the clipboard actually has content first. Also make sure the page has focus when you’re doing keyboard shortcuts.

Your code’s trying to read the clipboard right after Ctrl+A, but that only selects text - it doesn’t copy anything. You’re missing the actual copy command.

I ran into this same issue when scraping web forms. You need Ctrl+C after the selection, then wait for the clipboard to update:

// After your Ctrl+A sequence
await webPage.keyboard.down('Control');
await webPage.keyboard.press('c');
await webPage.keyboard.up('Control');

// Give it a moment to update
await webPage.waitForTimeout(100);

const clipboardData = await webPage.evaluate(() => {
  return navigator.clipboard.readText();
});

Also, double-check that there’s actually selectable content where you’re clicking. I’ve had better luck targeting specific elements instead of just clicking the viewport center.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.