8

I want to:

  • Login to a website
  • Save Cookies
  • Give user a choice to do A, B or C
  • A,B and C all require being logged in.
  • Each will open a FirefoxDriver and do their own thing

What i want to do, is login ONCE, save the cookies from that, and add them to any other FirefoxDriver i want to open.

Right now I'm trying to save the cookies in

public ReadOnlyCollection<Cookie> Cookies { get; set; }

which is the result of

WebDriver.Manage().Cookies.AllCookies;

Assuming login worked and cookies were saving in the above, I have this:

        WebDriver = new FirefoxDriver();
        WebDriver.Navigate().GoToUrl("http://www.example.com");

        if (cookies != null)
        {
            var s = WebDriver.Manage().Cookies;  //Logged out cookies
            WebDriver.Manage().Cookies.DeleteAllCookies(); //Delete all of them
            var sd = WebDriver.Manage().Cookies; //Make sure theyre deleted
            foreach (var cookie in cookies)
            {
                WebDriver.Manage().Cookies.AddCookie(cookie);
            }
            var ss = WebDriver.Manage().Cookies; 
            WebDriver.Navigate().GoToUrl("http://example.com/requiresloginpage");
        }

The problem is, howevering over "ss" in this case, gives this exception error

AllCookies = 'ss.AllCookies' threw an exception of type
'OpenQA.Selenium.WebDriverException'
base {System.Exception} = {"Unexpected problem getting cookies"}
InnerException = {"Cookie name cannot be null or empty string\r\nParameter name: name"}

I'm passing 8 cookies (total number when youre logged in) - and all of them seem set and ok. Not sure what I'm doing wrong

Alan Ciantar
  • 278
  • 1
  • 3
  • 14
  • You have inspected the contents of `s`, I assume? What do they contain Cookies that have all their values set? – Arran Jun 27 '13 at 14:30
  • Not sure what you mean but heres screenshots (are we allowed to post image links?) [[Not logged in (value of `s`): http://i.imgur.com/P5weWDb.png]] [[Logged in (the cookies im passing through) : http://i.imgur.com/Hwyn07h.png]] – Alan Ciantar Jun 27 '13 at 14:39
  • Inspect each cookie using the debugger, does it have a name as well as a value? – Arran Jun 27 '13 at 14:41
  • Yes, I'll continue looking at this later :) – Alan Ciantar Jun 27 '13 at 15:04

2 Answers2

8

In order to save cookies, you should tell selenium to use a specified profile. For some reason I can't get it to use my normal Chrome profile, but this solution will allow you to log in one time, and afterward, selenium will remember cookies.

ChromeOptions options = new ChromeOptions();
options.AddArguments(@"user-data-dir=C:\Users\YOU\AppData\Local\Google\Chrome\User Data\NAMEYOUCHOOSE");
//specify location for profile creation/ access
ChromeDriver driver = new ChromeDriver(options);

Simply put, this code creates a save location for a profile, which does include cookies. using this code, it is not necessary to write code that saves or loads cookies, Chrome will handle that.

Please note that the location where chrome saves your profiles may be different than mine, and I have only successfully used a directory that leads to the same location as my regular Chrome profile. This profile exists in the form of a folder, not a file.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
SJ10
  • 315
  • 1
  • 4
  • 12
0

Generally Selenium do not support cross-session cookies.

Most easy way is to use Serialization. You need to create wrapper class around selenium's cookie and make it serializable. And create class CookiesManager where will be 2 methods: SaveSession() -- to save and RestoreSession() - to restore from serialized file.

Another way is to save some cookies information into some temp cookies file. Like.... Csv or XML. Sample of this way you can see here: Keep user logged in - save cookies using web driver but only for c#.

Community
  • 1
  • 1
Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101