保持用户登录 – 使用Web驱动程序保存cookie

这就是我想用WebDriver执行的操作。

  1. 使用选中的复选框登录该站点,保持用户登录。
  2. 关闭浏览器。
  3. 再次转到该站点并确保该用户已登录。

问题是当我关闭驱动程序并重新打开它时,我没有登录。

我的代码看起来像这样 –

WebDriver myD = null; myD = new FirefoxDriver(); String URL = "https://www.eurostylelighting.com/protected/account/signin?ReturnUrl=%2f"; myD.navigate().to(URL); myD.findElement(By.cssSelector("input#txtEmailorRewards")).sendKeys("abc@yahoo.com"); myD.findElement(By.cssSelector("input#txtPassword")).sendKeys("abc"); myD.findElement(By.xpath(".//*[@id='accountSignIn']/dl[4]/dd/label/span")).click(); Set cookies = myD.manage().getCookies(); myD.close(); myD= new FirefoxDriver(); myD.navigate().to(URL); for(Cookie getCookie:cookies) myD.manage().addCookie(getCookie); 

  1. 您可以创建firefox /其他浏览器配置文件并使用它。 在这种情况下,所有cookie都将保存在此配置文件中。

  2. 您可以在浏览器打开后添加硬编码的cookie。 但在这种情况下,它们将是“静态的”。(每次会话都一样)

  3. 如果您只需要检查登录/注销,您可以手动将cookie保存到某些变量,从webdriver中删除cookie,刷新页面,添加cookie。

  4. 最简单的方法是使用序列化。 (创建自己的cookie类并将其保存/加载到hdd) 这是最好的选择!

  5. 您可以编写代码以在浏览器关闭之前将cookie保存到xml文件并在驱动程序启动时加载。 这是使用Linq在C#中实现此类function的示例:

 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 GetCookiesForUser(string user_name) { List cookiesList = new List(); 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 => (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; } } } } 

在这种情况下,可以用XML保存所有cookie,并通过附加的函数包装器从MyCookie加载到Cookie,并在需要时加载cookie。

顺便说一下这是cookies XML文件的例子:

           

user_name =“SomeName” – 是cookie配置文件名称

是的,您无法添加不属于您正在使用的域的cookie。 正如我在其他答案中提到的,根据域进行过滤并查看cookie是否属于测试域。 您可以执行以下操作:

 WebDriver myD = null; myD = new FirefoxDriver(); String URL = "https://www.eurostylelighting.com/protected/account/signin?ReturnUrl=%2f"; myD.navigate().to(URL); myD.findElement(By.cssSelector("input#txtEmailorRewards")).sendKeys("abc@yahoo.com"); myD.findElement(By.cssSelector("input#txtPassword")).sendKeys("abc"); myD.findElement(By.xpath(".//*[@id='accountSignIn']/dl[4]/dd/label/span")).click(); Set cookies = myD.manage().getCookies(); myD.close(); myD= new FirefoxDriver(); myD.navigate().to(URL); for(Cookie getCookie:cookies){ if(getCookie.domain.equals("Your domain")){ myD.manage().addCookie(getCookie); } } 

注意:您无法将cookie添加到localhost 。 如果是这种情况,请使用IP或其他内容

我已经复制了安德鲁的回答,但GetCookiesForUser方法只从他的例子中返回第一个cookie

  

局部变量cookiesList只包含一个元素。

我认为linq查询必须改为

 from e in xmlDoc.Root.Elements("cookies_list") where (string)e.Element("cookie").Attribute("user_name") == user_name from c in e.Elements("cookie") select new MyCookie 

主要思想是不通过几个user_names的cookie迭代,而是通过一个user_name的单独cookie