Search code examples
pythoncookieswebautomationplaywrightplaywright-python

python playwright - issue with adding cookies from file


I have cookies.json file where I stored cookies after login. I created the script to open page using that cookies, this is the code:

import json
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=False, slow_mo=50)
    context = browser.new_context(user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36')
    page = context.new_page()
    cookie_file = open('./cookies.json')
    cookies = json.load(cookie_file)
    context.addCookies(cookies)
    page.goto('https://www.url.com')
    try:
        page.wait_for_timeout(10000)
        page.close()
    except Exception as e:
        print("Error in playwright script.")
        page.close()

But I ve got this error:

AttributeError: 'ChromiumBrowserContext' object has no attribute 'addCookies'

This code was originally created in js, but when I transform it into python I've got this issue, any help?


Solution

  • Methods in python are snake case. context.add_cookies should do the work.