python 剧作家 - 从文件添加 cookie 的问题

python playwright - issue with adding cookies from file

我有 cookies.json 文件,我在其中存储登录后的 cookie。我创建了使用该 cookie 打开页面的脚本,这是代码:

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()

但是我遇到了这个错误:

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

这段代码最初是用js创建的,但是当我把它转换成python我遇到了这个问题,有什么帮助吗?

python 中的方法是 snake case。 context.add_cookies 应该做这项工作。