playwright-autoconsent
v1.0.4
Published
Easy integration of @duckduckgo/autoconsent with Playwright for automated cookie consent handling
Maintainers
Readme
playwright-autoconsent
Easy integration of @duckduckgo/autoconsent with Playwright for automated cookie consent handling in your tests.
Automatically detects and handles cookie consent popups from 100+ providers including Google, Cookiebot, OneTrust, Quantcast, and many more.
Features
- 🚀 Simple API - One line of code to handle cookie consent
- 🌍 Multi-language support - Works across all languages
- 🎯 100+ CMPs supported - Covers most major consent management platforms
- 🔧 Flexible - Opt-in, opt-out, or just detect
- 📦 Lightweight - Thin wrapper around battle-tested @duckduckgo/autoconsent
- 💪 TypeScript support - Full type definitions included
Installation
npm install playwright-autoconsent @duckduckgo/autoconsent playwrightOr with playwright-core (lighter):
npm install playwright-autoconsent @duckduckgo/autoconsent playwright-coreNote: Both @duckduckgo/autoconsent and playwright (or playwright-core) are peer dependencies and must be installed.
Quick Start
import { chromium } from 'playwright';
import { handleCookieConsent } from 'playwright-autoconsent';
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
// Automatically detect and reject all cookies
const result = await handleCookieConsent(page);
console.log('Consent handled:', result.handled);
console.log('CMP detected:', result.cmp);
console.log('Success:', result.success);
await browser.close();API
handleCookieConsent(page, options)
Handle cookie consent on an already-loaded page.
Parameters:
page- Playwright Page objectoptions- Configuration objectaction-'optIn'|'optOut'|null(default:'optOut')timeout- Timeout in ms (default:5000)enableCosmeticRules- Hide popups with CSS (default:true)debug- Enable logging (default:false)
Returns: Promise<ConsentResult>
const result = await handleCookieConsent(page, {
action: 'optOut', // Reject all cookies
timeout: 5000,
debug: true
});navigateAndHandleConsent(page, url, options)
Navigate to a URL and handle consent in one call.
import { navigateAndHandleConsent } from 'playwright-autoconsent';
const result = await navigateAndHandleConsent(
page,
'https://example.com',
{ action: 'optOut' }
);initAutoconsent(page, options)
Initialize autoconsent on a page (useful for SPAs).
import { initAutoconsent } from 'playwright-autoconsent';
// After page interaction
await page.click('#load-more');
const result = await initAutoconsent(page);Examples
Accept All Cookies
await handleCookieConsent(page, { action: 'optIn' });Just Detect (Don't Click Anything)
const result = await handleCookieConsent(page, { action: null });
if (result.handled) {
console.log(`Found ${result.cmp} consent popup`);
}With Remote Playwright
Works perfectly with remote Playwright instances:
import { chromium } from 'playwright';
import { navigateAndHandleConsent } from 'playwright-autoconsent';
const browser = await chromium.connect('ws://remote-playwright:9080');
const page = await browser.newPage();
await navigateAndHandleConsent(page, 'https://example.com');Debug Mode
const result = await handleCookieConsent(page, {
debug: true // Logs all autoconsent messages
});Supported CMPs
This library supports 100+ Consent Management Platforms including:
- Cookiebot
- OneTrust
- Quantcast
- TrustArc
- Evidon
- Osano
- CookieYes
- Termly
- And many more...
Full list: https://github.com/duckduckgo/autoconsent/tree/main/rules
How It Works
- Injects @duckduckgo/autoconsent into the page
- Detects which CMP (if any) is present
- Automatically clicks the appropriate buttons based on your action
- Returns the result
TypeScript
Full TypeScript support included:
import { Page } from 'playwright';
import { handleCookieConsent, ConsentResult } from 'playwright-autoconsent';
const result: ConsentResult = await handleCookieConsent(page, {
action: 'optOut',
timeout: 5000
});Limitations
- Cannot bypass CAPTCHAs - If a site shows a CAPTCHA, autoconsent won't help
- Requires JavaScript - Won't work if JavaScript is disabled
- Best effort - Some custom CMPs might not be detected
Related Projects
- @duckduckgo/autoconsent - The underlying consent detection library
- Consent-O-Matic - Browser extension for automatic consent handling
License
MIT
Contributing
Issues and PRs welcome! This is a thin wrapper around @duckduckgo/autoconsent.
For CMP detection issues or new CMP support, please contribute to the upstream project: https://github.com/duckduckgo/autoconsent
Credits
Built on top of the excellent @duckduckgo/autoconsent library by DuckDuckGo.
