Puppeteer clipboard text extraction not working

I am facing issues with clipboard functionality in my Puppeteer script. Here’s what I am attempting to accomplish:

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

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

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

    const page = await browser.newPage();
    await page.goto('');

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

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

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

    const textContent = await page.evaluate(() => {
        return navigator.clipboard.readText();
    });

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

    await browser.close();
})();

The problem is that even though I can manually paste text from my clipboard into the file, the script-related copying doesn’t function as expected. I’ve already allowed clipboard permissions and I am using Ctrl+A to select all text, but the clipboard reading part fails. What could be the issue?

I hit the same issue with Puppeteer clipboard stuff. You’re trying to read from the clipboard without copying anything first. After your Ctrl+A, you need await page.keyboard.press('Control+c'); before reading it. But there’s a better way that skips clipboard headaches entirely - use const textContent = await page.evaluate(() => window.getSelection().toString()); right after Ctrl+A. This is way more reliable since it doesn’t need clipboard permissions and works everywhere.

you’re missing the copy command after selecting the text. ctrl+a just selects everything but doesn’t actually copy it to the clipboard. add await page.keyboard.press('Control+c'); right after the ctrl+a part and before you read the clipboard. also make sure you’re awaiting clipboard.readText() properly since it’s async.

You’re selecting text but not actually copying it. The page.keyboard.down('Control') and page.keyboard.press('a') combo just selects everything - you need await page.keyboard.press('Control+c'); after that to copy it.

There’s also an issue with how you’re reading the clipboard. The navigator.clipboard.readText() inside page.evaluate() won’t work because the page context doesn’t have the same permissions as your Node.js context. Try await page.evaluate(async () => await navigator.clipboard.readText()) instead, or just grab the selected text directly with window.getSelection().toString() after hitting Ctrl+A.