Nicky Varghese
Nicky Varghese

Reputation: 31

How to clear browser cookies using Selenium Web Driver and C#

As with the title, how to clear browser cookies using Selenium Web Driver and C#?

IWebDriver driver = new FirefoxDriver();
driver.Manage().Cookies.DeleteAllCookies(); //delete all cookies
System.Threading.Thread.Sleep(5000);

I tried the above code but it is not deleting the session data.

Upvotes: 3

Views: 23239

Answers (2)

Chris Forbes
Chris Forbes

Reputation: 131

You can clear cookies with the following:

driver.Manage().Cookies.DeleteAllCookies();

Keep in mind that this will only clear cookies from the current domain. We work with some sites on Azure AD. To properly clear the session at the end of my test I run the delete method, navigate to their domain, and run the delete method again.

Upvotes: 7

Shubham Jain
Shubham Jain

Reputation: 17553

I think the code is :-

driver.manage().deleteAllCookies();

not

driver.Manage().Cookies.DeleteAllCookies();

OR

Trial with same firefox profile:

  1. driver.close();

  2. driver = new FirefoxDriver(SeleniumObject.firefoxprofile); // Used the profile with which previous session was started.

  3. driver.get(AppURL);

    • Browser gets closed.
    • The cookie is getting lost.

Trial with get(""):

  1. driver.get("");

  2. driver.get(AppURL);

Hope it will help you :)

Upvotes: 7

Related Questions