You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Playwright/C#测试中设置Cookie?迁移自Selenium遇问题

迁移Playwright/C#设置Cookie失败的问题排查与修复

问题背景

旧的Selenium/C#测试套件中,设置Cookie的代码运行正常:

private static void AddCookies()
{
    var cDom = TestUrl.Replace("https://", "")
                      .Replace("/", "");

    Cookie p1 = new(
        name: "hideWelcomeMessage",
        value: "true",
        domain: $"{cDom}",
        path: "/home",
        expiry: DateTime.Today.AddDays(1),
        secure: true,
        isHttpOnly: false,
        sameSite: "Lax"
    );
    Driver.Manage().Cookies.AddCookie(p1);

    Cookie p2 = new(
        name: "hideWelcomeWindow",
        value: "true",
        domain: $"{cDom}",
        path: "/home",
        expiry: DateTime.Today.AddDays(1),
        secure: true,
        isHttpOnly: false,
        sameSite: "Lax"
    );
    Driver.Manage().Cookies.AddCookie(p2);
}

但迁移到Playwright/C#后,以下代码既报错又无效:

var cDom = TestUrl.Replace("https://", "")
                  .Replace("/", "")
                  .Replace("www", "");

Cookie p1 = new()
{
    Name = "hideWelcomeMessage",
    Value = "true",
    Domain = $"{cDom}",
    Path = "/home",
    Expires = 86400,
    Secure = true,
    HttpOnly = false,
    SameSite = SameSiteAttribute.Lax
};

Cookie p2 = new()
{
    Name = "hideWelcomeWindow",
    Value = "true",
    Domain = $"{cDom}",
    Path = "/home",
    Expires = 86400,
    Secure = true,
    HttpOnly = false,
    SameSite = SameSiteAttribute.Lax
};

await Context.AddCookiesAsync([p1, p2]);
Page = await Context.NewPageAsync();

错误点分析

  • 域名处理错误:Playwright的Cookie.Domain需要与目标站点的完整域名完全匹配,你代码中额外移除了www前缀,会导致域名不匹配,Cookie无法被浏览器关联到目标站点。Selenium的原代码并未移除www,这是关键差异。
  • Expires参数类型错误:Playwright的Cookie.Expires是DateTime?类型,需要传入具体的过期时间对象(如DateTime.Today.AddDays(1)),而不是以秒为单位的数值(86400),类型不匹配会直接导致参数错误。
  • Cookie添加时机错误:Playwright添加Cookie前,需要确保Context已经加载过目标域名的页面(哪怕是空白页),否则Cookie无法被正确绑定到后续创建的页面。原代码先添加Cookie再创建页面,可能导致Cookie未被关联到新页面。

修正后的代码

// 保留完整域名,移除多余的www替换操作
var cDom = TestUrl.Replace("https://", "")
                  .Replace("/", "");

// 改用正确的Expires类型,和Selenium保持一致的过期时间逻辑
Cookie p1 = new()
{
    Name = "hideWelcomeMessage",
    Value = "true",
    Domain = cDom,
    Path = "/home",
    Expires = DateTime.Today.AddDays(1),
    Secure = true,
    HttpOnly = false,
    SameSite = SameSiteAttribute.Lax
};

Cookie p2 = new()
{
    Name = "hideWelcomeWindow",
    Value = "true",
    Domain = cDom,
    Path = "/home",
    Expires = DateTime.Today.AddDays(1),
    Secure = true,
    HttpOnly = false,
    SameSite = SameSiteAttribute.Lax
};

// 先创建页面并导航到目标域名的任意页面,确保Context有站点上下文
Page = await Context.NewPageAsync();
await Page.GotoAsync($"https://{cDom}");

// 再添加Cookie
await Context.AddCookiesAsync([p1, p2]);

// 重新导航到目标页面确保Cookie生效
await Page.GotoAsync($"https://{cDom}/home");

内容的提问来源于stack exchange,提问作者Kev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:19:54