Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...