In Selenium Webdriver Cookies post, we will learn what a cookie is and all details of Selenium Webdriver’s cookies API. Selenium webdriver can handle cookies with its built-in methods. We can add, delete, and query cookies.


Selenium Webdriver Cookies post is designed for SW test professionals who have some information about selenium web automation. At the end of this tutorial, you will know how to work with cookies in selenium webdriver.

Prerequisites for Selenium Webdriver Cookies

 Nice to have: Before starting to read Selenium Webdriver Synchronization post it is better to read previous selenium tutorials.


What is a cookie?

Cookies comprise of information about us and our preferences. A website places this information into our computer so that it can remember it in the future. Simply, a website can recognize you with cookies and look up your preferences. For instance, when we select language preference of a website, then the browser will save that information into a little document (cookie) on our PC. Next time we visit that website, a browser will read that cookie then remember our previous language preference, and changes website’s language automatically. As remembering your language preference, cookies can contain and remember any kind of information such as your visit time of a website, products that you added in your shopping basket, all the links that you clicked on a website, etc.

Cookie mechanism allows that the server (webpage) holds its own information about a user on the user’s PC. We can view the details of any cookie and also can delete them. Web users have to agree to let cookies be saved for them into their PC to websites can remember their cookies for better service. Also, in order to reduce cookie size, a unique ID is stored into that cookie and the rest of the personal data is stored in website’s servers. This kind of cookies is called third-party cookies. By the way, one of the most important cookies are the authentication cookies that provides login and account status to the web servers. The security of these cookies generally depends on the security of the related website and user’s web browser, also encryption of cookie’s data.

For more information about cookies please go below links:

Selenium Webdriver Cookies API

In selenium webdriver, we can query and interact with cookies with below built-in methods.

Interact and Query with Cookies


  • .getCookies()returns the list of all cookies
  • .getCookieNamed(“cookie name”)returns specific cookie according to its name
  • .addCookie(“cookie”)creates and adds a cookie
  • .deleteCookie(“cookie”)deletes specific cookie
  • .deleteCookieName(“cookie name”)deletes specific cookie according to its name
  • .deleteAllCookiesdelete all the cookies


We can create cookies with Cookie.Builder in an easy and convenient way instead of using a constructor to create a new cookie. You can build a new cookie by typing “new Cookie.Builder”. Its constructor gets some parameters such as name, value, path, domain, expiration date, is secure, etc. and at the last, we can finish the declaration with .build() to create a cookie with the builder. Name and value are mandatory fields for creating a cookie with Cookie.Builder.


  1. Go to
  2. Get all the Cookies and print the total number of Cookies.
  3. Click “I want mik with my cookie!”
  4. Get the cookie which name is “cookie” with getName() method and print its value.
  5. Assert that “cookie” value is “Chocolate_Chip”
  6. Copy above “cookie” as copiedCookie and print it and total cookie number.
  7. Create new cookie as “buildedCookie” with Builder and print it.
  8. Delete the “cookie”
  9. Print total cookie number after deleting “cookie”.
  10. Add new cookie by using “buildedCookie”
  11. Add new cookie by usingcopiedCookie”
  12. Print total cookie number after adding two cookies.
  13. Delete buildedCookie by using .deleteCookieName
  14. Delete copiedCookie by using .deleteCookie
  15. Print total cookie number after deleting two cookies.
  16. Delete all cookies.
  17. Print total cookie number after deleting all the cookies.
  18. Refresh page then select Gingerbread and click “I want milk with my cookie!” and click “Get a Cookie!” button.
  19. Get the cookie which name is “cookie” and assert that its value is “Ginderbread_milk” as shown below.


Test Code:

Console Output:


Example 2:

If you want to manually create a cookie and add, you can use below method. In below example, our aim is to create a new cookie and set next month to its expiration date.

Latest Updates website has some problems so I did an example also on you can find its code below.

Selenium Webdriver Tutorial Series