简体   繁体   English

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

[英]python playwright - issue with adding cookies from file

I have cookies.json file where I stored cookies after login.我有 cookies.json 文件,我在登录后存储了 cookies 文件。 I created the script to open page using that cookies, this is the code:我使用 cookies 创建了打开页面的脚本,代码如下:

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?此代码最初是在 js 中创建的,但是当我将其转换为 python 时,我遇到了这个问题,有什么帮助吗?

Methods in python are snake case. python 中的方法是蛇案例。 context.add_cookies should do the work. context.add_cookies应该做的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM