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
chrome.exe
's proccess

foreach (var process in Process.GetProcessesByName("chrome.exe"))
{
process.Kill();
}


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)

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("--lang=en");
options.AddArgument("--user-data-dir=" + chromeUserData);
options.LeaveBrowserRunning = false;
var driver = new ChromeDriver(options);
driver.Navigate().GoToUrl(chromeAdvancedSettings);

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")));
dropDown.SelectByIndex(4);
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']"));
elm.Click();
var waiter = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
waiter.Until(wd => wd.Url.StartsWith("chrome://settings"));
driver.Navigate().GoToUrl("chrome://newtab");

[Selenium documentation]