2015-05-26 31 views
2

这就是我想要使用WebDriver执行的操作。保持用户登录 - 使用网络驱动程序保存cookie 0

  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("[email protected]");   
    myD.findElement(By.cssSelector("input#txtPassword")).sendKeys("abc"); 
    myD.findElement(By.xpath(".//*[@id='accountSignIn']/dl[4]/dd/label/span")).click(); 
Set<Cookie> cookies = myD.manage().getCookies(); 
    myD.close(); 
    myD= new FirefoxDriver(); 
    myD.navigate().to(URL); 
for(Cookie getCookie:cookies) 
      myD.manage().addCookie(getCookie); 
+1

可能重复使用的cookie。请参阅[本](http://stackoverflow.com/questions/30292025/selenium-trying-to-log-in-with-cookies-can-only-set-cookies-for-current-doma) – Saifur

+0

感谢Saifur。我编辑了我的代码,但没有得到例外 - 您只能将Cookie设置为当前域。 –

回答

2

是的,您不能添加不属于您使用的域的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("[email protected]");   
myD.findElement(By.cssSelector("input#txtPassword")).sendKeys("abc"); 
myD.findElement(By.xpath(".//*[@id='accountSignIn']/dl[4]/dd/label/span")).click(); 
Set<Cookie> 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); 
    } 
} 

注意:您不能添加饼干本地主机。使用IP或其他东西,如果是这种情况

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

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

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

  4. 最简单的方法是使用序列化。(创建自己的cookies类别并将其保存/加载到/从硬盘驱动器)这是最好的选择!

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

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

    } 
} 

在这种情况下,有可能其在需要时从通过的myCookie附加功能,包装和负载饼干保存在XML和加载所有的cookie的cookie。

顺便说一句,这是例如饼干XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<body> 
    <cookies_list> 
    <cookie user_name="SomeName" c_name="6a64d0796e530a04069945d05c4074ca" c_value="yes" c_domain="www.marathonsportsbook.com" c_path="/" c_expiries="17.05.2057 15:41:44" c_secure="True" /> 
    <cookie user_name="SomeName" c_name="2b132c80be5271bcd9a0dddcc2f12c18" c_value="yes" c_domain="www.marathonsportsbook.com" c_path="/" c_expiries="17.05.2057 15:41:44" c_secure="True" /> 
    <cookie user_name="SomeName" c_name="PUNTER_KEY" c_value="A81B639C8F69931DAAD24DE4A8972632" c_domain=".marathonsportsbook.com" c_path="/" c_expiries="27.05.2016 15:41:44" c_secure="True" /> 
    <cookie user_name="SomeName" c_name="JSESSIONID" c_value="web2~F8D01B04BDE8C9702A1795521E06B218" c_domain="www.marathonsportsbook.com" c_path="/" c_expiries="28.05.2015 15:46:16" c_secure="True" /> 
    <cookie user_name="SomeName" c_name="afterLoginRedirectPath" c_value="&quot;https://www.marathonsportsbook.com/en/&quot;" c_domain="www.marathonsportsbook.com" c_path="/" c_expiries="28.05.2015 15:46:16" c_secure="True" /> 
    </cookies_list> 
</body> 

USER_NAME = “SomeName” - 是cookie的配置文件名称

0

我copypasted安德鲁的答案,但GetCookiesForUser方法只返回第一来自他的示例的cookie

<cookie user_name="SomeName" c_name="6a64d0796e530a04069945d05c4074ca"  c_value="yes" c_domain="www.marathonsportsbook.com" c_path="/" c_expiries="17.05.2057 15:41:44" c_secure="True" /> 

局部变量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的饼干重复,而是通过一个USER_NAME的单独的cookie