     [Answers](https://scrapfly.io/blog)   /  [playwright](https://scrapfly.io/blog/tag/playwright)   /  [How to save and load cookies in Playwright?](https://scrapfly.io/blog/answers/how-to-save-and-load-cookies-in-playwright)   # How to save and load cookies in Playwright?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Dec 05, 2022 1 min read [\#playwright](https://scrapfly.io/blog/tag/playwright) 

 [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-save-and-load-cookies-in-playwright "Share on LinkedIn")    

 

 

When web scraping, we might want to pause our scraping session by saving cookies and resume it later. Using Playwright, to save and load cookies we need to refer to the `context` object which has methods `cookies()` and `add_cookies()`:

python```python
import json
from pathlib import Path

from playwright.sync_api import sync_playwright

with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False)

    # To save cookies to a file first extract them from the browser context:
    context = browser.new_context(viewport={"width": 1920, "height": 1080})
    page = context.new_page()
    page.goto("https://httpbin.dev/cookies/set/mycookie/myvalue")
    cookies = context.cookies()
    Path("cookies.json").write_text(json.dumps(cookies))

    # Then, we can restore cookies from file:
    context = browser.new_context(viewport={"width": 1920, "height": 1080})
    context.add_cookies(json.loads(Path("cookies.json").read_text()))
    page = context.new_page()
    page.goto("https://httpbin.dev/cookies")
    print(context.cookies())  # we can test whether they were set correctly
    # will print:
    [
        {
            "sameSite": "Lax",
            "name": "mycookie",
            "value": "myvalue",
            "domain": "httpbin.dev",
            "path": "/",
            "expires": -1,
            "httpOnly": False,
            "secure": False,
        }
    ]
```





 

    



Scale Your Web Scraping

Anti-bot bypass, browser rendering, and rotating proxies, all in one API. Start with 1,000 free credits.

  No credit card required  1,000 free API credits  Anti-bot bypass included 

 [Start Free](https://scrapfly.io/register) [View Docs](https://scrapfly.io/docs/onboarding) 

 Not ready? Get our newsletter instead. 

 

## Explore this Article with AI

 [ ChatGPT ](https://chat.openai.com/?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-save-and-load-cookies-in-playwright) [ Gemini ](https://www.google.com/search?udm=50&aep=11&q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-save-and-load-cookies-in-playwright) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-save-and-load-cookies-in-playwright) [ Perplexity ](https://www.perplexity.ai/search/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-save-and-load-cookies-in-playwright) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-save-and-load-cookies-in-playwright) 



 ## Related Articles

 [  

 http 

### How to Handle Cookies in Web Scraping

Introduction to cookies in web scraping. What are they and how to take advantage of cookie process to authenticate or se...

 

 ](https://scrapfly.io/blog/posts/how-to-handle-cookies-in-web-scraping) [     

### Advanced Proxy Connection Optimization Techniques

Proxy performance bottlenecks often stem from inefficient connection management rather than bandwidth limitations or ser...

 

 ](https://scrapfly.io/blog/posts/advanced-proxy-connection-optimization-techniques) [  

 http 

### Guide to SSL Errors: What do they mean and how to fix them

Overview of SSL errors - what are they, what are common issues and how to resolve them.

 

 ](https://scrapfly.io/blog/posts/guide-to-ssl-error-meaning-and-fixes) 

  ## Related Questions

- [ Q How to save and load cookies in Python requests? ](https://scrapfly.io/blog/answers/save-and-load-cookies-in-requests-python)
- [ Q How to save and load cookies in Puppeteer? ](https://scrapfly.io/blog/answers/how-to-save-and-load-cookies-in-puppeteer)
- [ Q How to save and load cookies in Selenium? ](https://scrapfly.io/blog/answers/how-to-save-and-load-cookies-in-selenium)
- [ Q How to load local files in Playwright? ](https://scrapfly.io/blog/answers/how-to-load-local-files-in-playwright)
 
  



   



 Run headless browsers at scale, **1,000 free credits** [Start Free](https://scrapfly.io/register)