Cookies in Selenium

Let’s see, How to handle Cookies in Selenium. First, let’s understand, What are Cookies?

An HTTP cookie is a small piece of data sent from a website and stored in a user’s web browser while the user is browsing that website.


Every time the user loads the website, the browser sends the cookie back to the server to notify the website of the user’s previous activity.

Almost all websites use cookies in one form or another. Cookies are a way of remembering users and their interaction with the site by storing information in the cookie file as key-value pairs.


When testing a website with Selenium WebDriver, sometimes it is necessary to handle cookies, such as creating new cookies, updating existing cookies with new information or deleting cookies.

In this WebDriver tutorial, we look at handling cookies in WebDriver.


Add Cookies with Selenium WebDriver

In Selenium webdriver, we use selenium inbuilt method “addCookie(cookie)” to pass cookies name to the website server.

Here we need to pass the cookies name as the parameter to this method. The purpose of this method is to add a specific cookie into cookies. If the cookie’s domain name is left blank, it is assumed that the cookie is meant for the domain of the current document.


Now let’s see how can we get the cookies name and value stored in our browser.

Firefox  – Select the ‘Tools->Options’ menu item and click on the ‘Privacy’ tab. Click the ‘remove individual cookies’ link, and you will be shown a list of all cookies stored by Firefox.  From here you can get cookies name and value.


Google Chrome – Choose ‘Settings’ on the ‘Customize and control’ menu, click ‘Show advanced settings’ at the bottom of the screen. In the ‘Privacy’ section, click on the ‘Content settings…’ button then click the ‘all cookies and site data’ button.

Now we will see, How can we add the specific cookies for the website or web domain using selenium.


In Our example, we will add the one of cookie to Flipkart domain while calling the Flipkart in our Test script.

Cookies using Selenium


You can see in this example we are passing the cookies to Flipkart while calling the URL and after that, we are getting all cookies that Flipkart is sending and printing it.

Now let’s see the Output as well.


Cookies in Selenium


In this output we can see clearly, It’s getting printed our cookies value as well. So this is the way how can we Add the cookies in selenium.

0 Comment

Leave a Reply