Skip to content

Instantly share code, notes, and snippets.

@muditlambda
Created July 28, 2021 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muditlambda/bf387b8dd571a36729796728d37390ad to your computer and use it in GitHub Desktop.
Save muditlambda/bf387b8dd571a36729796728d37390ad to your computer and use it in GitHub Desktop.
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandleCookies {
public static void main(String args[]) {
System.setProperty("webdriver.chrome.driver", "ChromeDriver Path");
WebDriver driver = new ChromeDriver();
String url ="https://www.lambdatest.com/";
driver.get(url);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
//For creating a new cookie we should pass the name of the cookie and its value
Cookie cname = new Cookie("myCookie", "12345678999");
driver.manage().addCookie(cname);
Set<Cookie> cookiesList = driver.manage().getCookies();
for(Cookie getcookies :cookiesList) {
System.out.println(getcookies );
}
//delete the newly created cookie
driver.manage().deleteCookie(cname);
Set<Cookie> cookiesListNew = driver.manage().getCookies();
for(Cookie getcookies :cookiesListNew) {
System.out.println(getcookies );
}
driver.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment