I'm writing a Python script using playwright. I would like to allow the cookies before taking the associated snapshot of any website. Do you know how to handle this ? Thank you !
Asked
Active
Viewed 225 times
1 Answers
0
You could easily do that using setCookies function check out this one,
import asyncio
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch()
context = await browser.new_context()
# Enable cookies
await context.set_cookies([
{
"name": "cookie_name",
"value": "cookie_value",
"domain": "example.com"
}
])
# Navigate to website and take a snapshot
page = await context.new_page()
await page.goto('https://example.com')
await page.screenshot(path='example.png')
await browser.close()
asyncio.run(main())
-
Dear @Abdullahi Ashik Md. Arefin, thank you for your quick answer. Unfortunately, I receive this error : `code` await context.set_cookies([ AttributeError: 'BrowserContext' object has no attribute 'set_cookies' `code` – deezer tsix Mar 19 '23 at 15:05