简体   繁体   English

在 selenium webdriver C# 中为 chrome 设置 cookies

[英]Setting cookies in selenium webdriver C# for chrome

Does anyone know how to set the expiry date for a cookie using Selenium C# and why an extra character is added to the domain specified?有谁知道如何使用 Selenium C# 设置 cookie 的到期日期以及为什么将额外的字符添加到指定的域?

Any issues with Chrome? Chrome 有任何问题吗? And is there a desired capabilities or user preference to be added?是否需要添加所需的功能或用户偏好?

cookie1Dictionary.Add("domain","somesite.com ")
cookie1Dictionary.Add("path", "/");
cookie1Dictionary.Add("expiry", expirationdate.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff'Z'"));            

For some reason the expiry data is null after using the call to出于某种原因,使用调用后过期数据为 null

driver.Manage().AllCookies.AddCookie(cookie1Dictionary);

I'm trying to bypass sso login....我正在尝试绕过 sso 登录....

So expiry is null and the domain is displayed as .somesites.com it's adding an additional character所以过期时间是 null 域显示为.somesites.com它添加了一个额外的字符

I expected to add the cookies with all data specified.我希望添加指定所有数据的 cookies。

There are the following overloaded constructors for Cookie : Cookie有以下重载构造函数:

public Cookie(string name, string value)
public Cookie(string name, string value, string path)
public Cookie(string name, string value, string path, DateTime? expiry)
public Cookie(string name, string value, string domain, string path, DateTime? expiry)
public Cookie(string name, string value, string domain, string path, DateTime? expiry, bool secure, bool isHttpOnly, string sameSite)

One way to add a cookie with selenium webdriver is the following:使用 selenium webdriver 添加 cookie 的一种方法如下:

Cookie cookie = new (name: "key",
                     value: "value",
                     domain: "stackoverflow.com",
                     path: "/",
                     expiry: DateTime.Today.AddDays(1),
                     secure: true,
                     isHttpOnly: true,
                     sameSite: "Strict");

driver.Manage().Cookies.AddCookie(cookie);

with this snippet you will get this output:使用此代码段,您将获得此 output: 在此处输入图像描述

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

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