将Selenium Cookie复制到CookieContainer

时间:2018-05-16 20:17:26

标签: c# selenium cookies

我试过了:

   ID        Name              Multi Name
0   1    John Doe      John Doe, Jane Doe
1   1    Jane Doe      John Doe, Jane Doe
2   2  John Smith  John Smith, Jane Smith
3   2  Jane Smith  John Smith, Jane Smith
4   3   Jack Hill               Jack Hill
5   4   Jill Hill               Jill Hill

但它在foreach (OpenQA.Selenium.Cookie cook in driver.Manage().Cookies.AllCookies) { System.Net.Cookie cookie = new System.Net.Cookie(); cookie.Name = cook.Name; cookie.Value = cook.Value; cookie.Domain = cook.Domain; header.Add(cookie); }

给了我一个NullReference

我试图将我的Selenium驱动程序的Cookie设置为header.Add(cookie);以便在CookieContainer中使用。

2 个答案:

答案 0 :(得分:0)

这就是您的操作方式。

   WebClientEx myWebClient = new WebClientEx(new CookieContainer());
   CookieCollection cc = new CookieCollection();
   foreach (OpenQA.Selenium.Cookie cook in driver.Manage().Cookies.AllCookies)
   {
        System.Net.Cookie cookie = new System.Net.Cookie();
        cookie.Name = cook.Name;
        cookie.Value = cook.Value;
        cookie.Domain = cook.Domain;
        cc.Add(cookie);
   }
   myWebClient.CookieContainer.Add(cc);

您需要将此扩展名用于webclient:

public class WebClientEx : WebClient
{
    public WebClientEx(CookieContainer container)
    {
        this.container = container;
    }

    public CookieContainer CookieContainer
    {
        get { return container; }
        set { container = value; }
    }

    private CookieContainer container = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest r = base.GetWebRequest(address);
        var request = r as HttpWebRequest;
        if (request != null)
        {
            request.CookieContainer = container;
        }
        return r;
    }

    protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
    {
        WebResponse response = base.GetWebResponse(request, result);
        ReadCookies(response);
        return response;
    }

    protected override WebResponse GetWebResponse(WebRequest request)
    {
        WebResponse response = base.GetWebResponse(request);
        ReadCookies(response);
        return response;
    }

    private void ReadCookies(WebResponse r)
    {
        var response = r as HttpWebResponse;
        if (response != null)
        {
            CookieCollection cookies = response.Cookies;
            container.Add(cookies);
        }
    }
}

我在这里找到的扩展名:Using CookieContainer with WebClient class

答案 1 :(得分:0)

循环访问网络驱动程序中的 selenium cookie 并将它们转换为 System.Net cookie:

    var cookieContainer = new CookieContainer();
    foreach (var cookie in driver.Manage().Cookies.AllCookies)
    {
        System.Net.Cookie netcookie = new System.Net.Cookie()
        {
            Domain = cookie.Domain,
            HttpOnly = cookie.IsHttpOnly,
            Name = cookie.Name,
            Path = cookie.Path,
            Secure = cookie.Secure,
            Value = cookie.Value,
        };
        if (cookie.Expiry.HasValue)
            netcookie.Expires = cookie.Expiry.Value;
        cookieContainer.Add(netcookie);
    }