JayGatsby JayGatsby - 26 days ago 9
C# Question

How can I remove google chrome cookies

I'm trying to remove cookies of chrome browser. Firstly I declared the path

string chromeLocation1 = "C:\\Users\\" + Environment.UserName.ToString() + "\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Local Storage";

When I try to run my remove code "The file is in use by another program or user" error appears. So I tried to kill
's proccess

foreach (var process in Process.GetProcessesByName("chrome.exe"))

But now it gives me "Access Denied" error even I run it as administrator. What should I do to remove these cookies?

Answer Source

You can delete all cookies with selenium framework.

1) Install selenium framework - Selenium WebDriver and Selenium WebDriver Support Classes (the easiest way to do this is by using NuGet)


2) Use the following code to delete all cookies:

var chromeUserData = "C:\\Users\\" + Environment.UserName.ToString(CultureInfo.InvariantCulture) + "\\AppData\\Local\\Google\\Chrome\\User Data";
var chromeAdvancedSettings = "chrome://settings/clearBrowserData";
var options = new ChromeOptions();
options.AddArgument("--user-data-dir=" + chromeUserData);
options.LeaveBrowserRunning = false;
var driver = new ChromeDriver(options);

var frame = driver.FindElement(By.XPath("//iframe[@src='chrome://settings-frame/clearBrowserData']"));
var frameDriver = driver.SwitchTo().Frame(frame);
var dropDown = new SelectElement(frameDriver.FindElement(By.Id("clear-browser-data-time-period")));
var elm = driver.FindElement(By.Id("delete-cookies-checkbox"));
if (!elm.Selected) elm.Click();
elm = driver.FindElement(By.XPath("//button[@id='clear-browser-data-commit']"));
var waiter = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
waiter.Until(wd => wd.Url.StartsWith("chrome://settings"));

[Selenium documentation]