简体   繁体   English

C#/ASP.NET Selenium WebDriver - 重用 Cookie

[英]C#/ASP.NET Selenium WebDriver - Re-using Cookies

I want to:我想要:

  • Login to a website登录网站
  • Save Cookies保存饼干
  • Give user a choice to do A, B or C让用户选择做 A、B 或 C
  • A,B and C all require being logged in. A、B 和 C 都需要登录。
  • Each will open a FirefoxDriver and do their own thing每个人都会打开一个FirefoxDriver并做自己的事情

What i want to do, is login ONCE , save the cookies from that, and add them to any other FirefoxDriver i want to open.我想要做的是登录一次,从中保存 cookie,并将它们添加到我想打开的任何其他 FirefoxDriver。

Right now I'm trying to save the cookies in现在我正在尝试将 cookie 保存在

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:假设登录成功并且cookies保存在上面,我有这个:

        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问题是,但是在这种情况下超过“ss”,会给出这个异常错误

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.我正在传递 8 个 cookie(登录时的总数)-它们似乎都设置好了。 Not sure what I'm doing wrong不知道我做错了什么

In order to save cookies, you should tell selenium to use a specified profile.为了保存 cookie,您应该告诉 selenium 使用指定的配置文件。 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.出于某种原因,我无法让它使用我的普通 Chrome 配置文件,但此解决方案将允许您登录一次,之后 selenium 将记住 cookie。

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.简而言之,此代码为配置文件创建了一个保存位置,其中包含 cookie。 using this code, it is not necessary to write code that saves or loads cookies, Chrome will handle that.使用此代码,无需编写保存或加载 cookie 的代码,Chrome 会处理。

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.请注意,chrome 保存您的配置文件的位置可能与我的不同,我只成功使用了一个目录,该目录与我的常规 Chrome 配置文件的位置相同。 This profile exists in the form of a folder, not a file.此配置文件以文件夹的形式存在,而不是文件。

Generally Selenium do not support cross-session cookies.通常 Selenium 不支持跨会话 cookie。

Most easy way is to use Serialization.最简单的方法是使用序列化。 You need to create wrapper class around selenium's cookie and make it serializable.您需要围绕 selenium 的 cookie 创建包装类并使其可序列化。 And create class CookiesManager where will be 2 methods: SaveSession() -- to save and RestoreSession() - to restore from serialized file.并创建 CookiesManager 类,其中将有 2 个方法:SaveSession() - 保存和 RestoreSession() - 从序列化文件中恢复。

Another way is to save some cookies information into some temp cookies file.另一种方法是将一些 cookie 信息保存到一些临时 cookie 文件中。 Like.... Csv or XML.像.... CSV 或 XML。 Sample of this way you can see here: Keep user logged in - save cookies using web driver but only for c#.您可以在此处看到这种方式的示例: 保持用户登录 - 使用网络驱动程序保存 cookie,但仅适用于 c#。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM