0

我有 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 时,我遇到了这个问题,有什么帮助吗?

4

1 回答 1

0

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

于 2021-04-16T11:16:26.023 回答