Selenium Tips: Efficiently removing cookies

Posted by Santiago Suarez Ordoñez in Selenium Resources

We all know that when your tests run in firefox, things are easy. Even for sessions and cookies. Each time you run the start() method, Selenium will generate a clean Firefox profile and you can forget about previous tests or sessions. Well, life is not always easy, and we don't always have to test just Firefox... When IE comes to the game, Selenium can't create clean profiles and cookies from previous tests will remain for future ones, causing unexpected issues. For this, Selenium API's has 2 methods you should use:

sel.delete_cookie(name, optionsString)

This method is useful when you only want to remove a specific cookie. The parameters it receives are sometimes hard to write and can take some time to actually make it work the way you need.


This one is my personal favorite. It just blows away any cookie that the current domain could have created in your browser. But wait! I said "current domain", not all domains. So, if your tests go though several domains, and you want to keep things clean, you'll need something like the following:

def clean_history(sel, domains):
    temp = sel.get_location()
    for domain in domains:

This is a python function you can add anywhere, and by calling it with the selenium browser as the first parameter and a list of all domains used in the second one, will make you session clean without too much hassle. Off course you can always make this part of your own version of the selenium client, for then being able to call:


or even better, make selenium keep a record of each domain and then you can call:


But that will have to go in another TOTW.

Discuss: Selenium Tips: Efficiently removing cookies

Free Trial

Get access to a free 14-day trial version, or contact Sales for more information.