Reputation: 16299
I'm having trouble removing/adding cookies in Selenium. I'm using Windows 7 and FireFox 25.0.1. My code looks like this:
Instance = new FirefoxDriver();
Instance.Manage().Window.Maximize();
var _cookies = Instance.Manage().Cookies.AllCookies;
Instance.Manage().Cookies.DeleteAllCookies();
foreach(Cookie cookie in _cookies)
{
Instance.Manage().Cookies.AddCookie(cookie);
}
var _newCookies = Instance.Manage().Cookies.AllCookies; //boom
On that last line I get the exception "Unexpected problem getting cookies." I've tried several variants of the above code and the same problem occurs the second time I call AllCookies
-- even after closing and reopening the browser and calling GoToUrl(mysite)
and re-adding the cookies (The browser was on mysite
when I saved the cookies).
I've checked the cookies collection before accessing it, and they all have name/value pairs.
Has anyone managed to use the cookie API successfully in Selenium for C# or can say what I'm doing wrong?
Upvotes: 4
Views: 30172
Reputation: 3438
You can only add cookies if your browser is displaying a page of the domain you want to drop the cookie on.
you don't appear to have navigated to a URL before dropping cookies.
Upvotes: 3