How to get the cookie value in Playwright Java?

Cookies are small pieces of data stored in a user’s web browser while browsing a website. Cookies can be used for various purposes, such as identifying users, storing user preferences, and tracking user activity.

This article will focus on getting a cookie value in Playwright using Java.

Create a BrowserContext and a new Page

We can create a new browser context using the browser.newContext() and a new page using the browserContext.newPage() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
 
public class CodekruTest {
 
    public static void main(String[] args) {
 
        Playwright playwright = Playwright.create();
        Browser browser = playwright.chromium().launch();
 
        // creating a BrowserContext
        BrowserContext browserContext = browser.newContext();
 
        // Creating a new Page
        Page page = browserContext.newPage();
 
        // code
 
        // closing the instances
        browser.close();
        playwright.close();
 
    }
 
}
Navigating to the URL

Please go to the website address for which you need to retrieve the cookies.

Get all cookies

BrowserContext provides us with a cookies() method that returns all cookies associated with all pages of the browser context.

1
List<Cookie> cookies = browserContext.cookies();
Get a particular cookie value

We can then iterate through all the cookies and select the one we want.

1
2
3
4
5
6
7
for (Cookie cookie : cookies) {
    if (cookie.name.contains("your_cookie_name")) {
        System.out.println("Cookie name: " + cookie.name);
        System.out.println("Cookie value: " + cookie.value);
    }
 
}
Get cookies from a particular URL

A BrowserContext can have multiple pages. Using browserContext.cookies() retrieves cookies from all the pages, but we can also retrieve the cookies from a single page by using browserContext.cookies(url).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.Cookie;
 
public class CodekruTest {
 
    public static void main(String[] args) {
 
        Playwright playwright = Playwright.create();
        Browser browser = playwright.chromium().launch();
 
        // creating a BrowserContext
        BrowserContext browserContext = browser.newContext();
 
        // Creating new Pages
        Page page1 = browserContext.newPage();
        Page page2 = browserContext.newPage();
 
        page1.navigate("https://testkru.com/Elements/TextFields");
        page2.navigate("https://www.makemytrip.com");
 
        List<Cookie> cookies = browserContext.cookies("https://www.makemytrip.com");
 
        for (Cookie cookie : cookies) {
            System.out.println("Cookie name: " + cookie.name);
            System.out.println("Cookie value: " + cookie.value);
        }
 
        // closing the instances
        browser.close();
        playwright.close();
 
    }
 
}

We hope that you have liked the article. If you have any doubts or concerns, please write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *