Home node.js How to save and reuse cookies in javascript + selenium?

How to save and reuse cookies in javascript + selenium?

Author

Date

Category

Interested in using the pure selenium-webdriver module. So that it would be possible to log into the tested site only once, and then reuse the saved cookies. So that these saved cookies can be added to the newly launched browser instance.


Answer 1, authority 100%

Personally, I implemented it by writing a separate module-exporter-importer of cookies to / from an XML file.

Here is C # code using LINQ

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace Ifrit
{
  public class CookiesManager
  {
    XDocument xmlDoc;
    string xml_path;
    public CookiesManager ()
    {
      xml_path = ParamsLib.BrwsrOptions.BrowserCookiesFile;
      xmlDoc = new XDocument ();
      if (File.Exists (xml_path))
      {
        xmlDoc = XDocument.Load (xml_path);
      }
      else
      {
        var xmlBodyNode = new XElement ("body", "");
        var xmlCList = new XElement ("cookies_list", "");
        xmlBodyNode.Add (xmlCList);
        xmlBodyNode.Save (xml_path);
        xmlDoc = XDocument.Load (xml_path);
      }
    }
    public List & lt; MyCookie & gt; GetCookiesForUser (string user_name)
    {
      List & lt; MyCookie & gt; cookiesList = new List & lt; MyCookie & gt; ();
      try
      {
        cookiesList = (from e in xmlDoc.Root.Elements ("cookies_list")
                let cookie = e.Element ("cookie")
where (string) cookie.Attribute ("user_name") == user_name
                select new MyCookie
                {
                  name = (string) cookie.Attribute ("c_name"),
                  value = (string) cookie.Attribute ("c_value"),
                  domain = (string) cookie.Attribute ("c_domain"),
                  path = (string) cookie.Attribute ("c_path"),
                  expiries = (string) cookie.Attribute ("c_expiries"),
                  secure = (string) cookie.Attribute ("c_secure"),
                }). ToList ();
      }
      catch
      {}
      return cookiesList;
    }
    public void AddCookiesForUser (string username, string cookieName, string cookieValue, string domainName, string path, string expiries, string secure)
    {
      var xmlNode = new XElement ("cookie", new XAttribute ("user_name", username),
                new XAttribute ("c_name", cookieName),
                new XAttribute ("c_value", cookieValue),
                new XAttribute ("c_domain", domainName),
                new XAttribute ("c_path", path),
                new XAttribute ("c_expiries", expiries),
                new XAttribute ("c_secure", secure)
      );
      xmlDoc.Element ("body"). Element ("cookies_list"). Add (xmlNode);
    }
    public void Save ()
    {
      xmlDoc.Save (xml_path);
    }
    public void removeCookieForUser (string username)
    {
      try
      {
        xmlDoc.Element ("body"). Element ("cookies_list"). Descendants ("cookie")
.Where (x = & gt; (string) x.Attribute ("user_name") == username)
                  .Remove ();
      }
      catch
      {
      }
    }
    public class MyCookie
    {
      public string name {get; set; }
      public string value {get; set; }
      public string domain {get; set; }
      public string path {get; set; }
      public string expiries {get; set; }
      public string secure {get; set; }
    }
  }
}

In this case, it is possible to save cookies and load them manually at the right time according to the desired user profile. An example of the XML file itself:

& lt;? xml version = "1.0" encoding = "utf-8"? & gt;
& lt; body & gt;
 & lt; cookies_list & gt;
  & lt; cookie user_name = "SomeName" c_name = "6a64d0796e530a04069945d05c4074ca" c_value = "yes" c_domain = "www.marathonsportsbook.com" c_path = "/" c_expiries = "05/17/2057 15:41:44" c_secure = "True & gt;
  & lt; cookie user_name = "SomeName" c_name = "2b132c80be5271bcd9a0dddcc2f12c18" c_value = "yes" c_domain = "www.marathonsportsbook.com" c_path = "/" c_expiries = "05/17/2057 15:41:44" c_secure & gt;
  & lt; cookie user_name = "SomeName" c_name = "PUNTER_KEY" c_value = "A81B639C8F69931DAAD24DE4A8972632" c_domain = ". marathonsportsbook.com" c_path = "/" c_expiries = "05/27/2016 15:41:44" c / & & secure ;
  & lt; cookie user_name = "SomeName" c_name = "JSESSIONID" c_value = "web2 ~ F8D01B04BDE8C9702A1795521E06B218" c_domain = "www.marathonsportsbook.com" c_path = "/" c_expiries = "05/28/2015 15:46:16 "/ & gt;
& lt; cookie user_name = "SomeName" c_name = "afterLoginRedirectPath" c_value = "& amp; quot; https: //www.marathonsportsbook.com/en/& quot;" c_domain = "www.marathonsportsbook.com" c_path = "/" c_expiries = "05/28/2015 15:46:16" c_secure = "True" / & gt;
 & lt; / cookies_list & gt;
& lt; / body & gt;

user_name = “SomeName” – cookie profile name

You can do the same, only in your own language.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions