pradeepraj
pradeepraj

Reputation: 11

specific cookie is present or not using selenium webdriver c#

How to find cookie is present or not using selenium webdriver ?

 public bool CheckIfCookiePresent(bool empty)
 {
     Browser.Driver.Manage().Cookies.GetCookieNamed("cookie name");
     {
         return true;
     }
     return false;
 }

Upvotes: 1

Views: 3136

Answers (1)

Arran
Arran

Reputation: 25076

Two ways, both practically identical. First one using LINQ, second using what you've already got but modified slightly:

public bool IsCookiePresent(string cookieName)
{
    return Driver.Manage().Cookies.AllCookies.Any(c => c.Name.Equals(cookieName, StringComparison.OrdinalIgnoreCase));
}

vs:

public bool IsCookiePresent(string cookieName)
{
    return Driver.Manage().Cookies.GetCookieNamed(cookieName) != null;
}

Upvotes: 4

Related Questions