For BE/B.Tech/BCA/MCA/ME/M.Tech Major/Minor Project for CS/IT branch at minimum price Text Message @ 9424820157

How to Handle Cookies in Selenium

  Cookies Handling in Selenium


Cookies are used to track user in website. A cookie is a small piece of data sent from a website and stored on the user’s computer. Cookies also recognize users returning to a website and loading the previously stored information. It is used to store the user’s identity and track the user’s journey through the website’s pages.

How to add a cookie in Selenium

Below is the sample code to add a cookie by Selenium java:
package com.cookies;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class AddCookie {
    
    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();
        
        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        
        driver.get("https://www.google.com");
        
        Cookie cookie = new Cookie("newCookie","goformule");
        driver.manage().addCookie(cookie);
        
        Thread.sleep(2000);
        driver.quit();
        
    }
}

How to get a cookie in Selenium:

Below is the sample code to get a cookie by Selenium java:

package com.cookies;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class GetCookie {

    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        driver.get("https://www.google.com");

        Cookie cookie = new Cookie("newCookie", "goformule");
        driver.manage().addCookie(cookie);
        
        Cookie cookieName = driver.manage().getCookieNamed("newCookie");
        System.out.println("Cookie Name: "+cookieName.getValue());
        
        Thread.sleep(2000);
        driver.quit();
    }

}


How to get all cookies in Selenium:

Below is the sample code to get all cookies by Selenium java:
package com.cookies;

import java.util.Iterator;
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 GetAllCookies {
    
    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        driver.get("https://www.google.com");
        
        Set<Cookie> set = driver.manage().getCookies();
        
        Iterator<Cookie> it = set.iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
        
        Thread.sleep(2000);
        driver.quit();
    }

}


How to delete a cookie in Selenium

Below is the sample code to delete a cookie by Selenium java:

package com.cookies;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DeleteCookie {
    
    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        driver.get("https://www.google.com");
        
        Cookie cookie = new Cookie("newCookie","goformule");
        driver.manage().addCookie(cookie);

        driver.manage().deleteCookie(cookie);
        System.out.println("Cookies Deleted");

        Thread.sleep(2000);
        driver.quit();
    }

}


How to delete a cookie by name in Selenium

Below is the sample code to delete a cookie by name by Selenium java:

package com.cookies;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DeleteCookieByName {
    
    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        driver.get("https://www.google.com");
        
        driver.manage().deleteCookieNamed("goformule");
        System.out.println("Cookies Deleted");

        Thread.sleep(2000);
        driver.quit();
    }

}

How to delete all cookies in Selenium

Below is the sample code to delete all cookies by Selenium java:
package com.cookies;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class DeleteAllCookies {

    static String projectLocation = System.getProperty("user.dir");

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", projectLocation + "/driver/chromedriver.exe");

        WebDriver driver = new ChromeDriver();

        // maximize window
        driver.manage().window().maximize();

        // delete all cookies
        driver.manage().deleteAllCookies();

        // dynamic wait
        driver.manage().timeouts().pageLoadTimeout(50, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        driver.get("https://www.google.com");

        driver.manage().deleteAllCookies();
        System.out.println("Cookies Deleted");

        Thread.sleep(2000);
        driver.quit();
    }
}


Please go through below tutorials:


Mule 4 Tutorials

DEPLOY TO CLOUDHUB C4E CLIENT ID ENFORCEMENT CUSTOM POLICY RABBIT MQ INTEGRATION
XML TO JSON WEBSERVICE CONSUMER VM CONNECTOR VALIDATION UNTIL SUCCESSFUL
SUB FLOW SET & REMOVE VARIABLE TRANSACTION ID SCATTER GATHER ROUND ROBIN
CONSUME REST WEBSERVICE CRUD OPERATIONS PARSE TEMPLATE OBJECT TO JSON LOAD STATIC RESOURCE
JSON TO XML INVOKE IDEMPOTENT FILTER FOR EACH FLAT TO JSON
FIXWIDTH TO JSON FIRST SUCCESSFUL FILE OPERATIONS EXECUTE ERROR HANDLING
EMAIL FUNCTIONALITY DYNAMIC EVALUATE CUSTOM BUSINESS EVENT CSV TO JSON COPYBOOK TO JSON
CHOICE ASYNC

Widely used Connectors in Mule 3

CMIS JETTY VM CONNECTOR SALESFORCE POP3
JMS TCP/IP WEBSERVICE CONSUMER QUARTZ MONGO DB
FILE CONNECTOR DATABASE CONNECTOR


Widely used Scopes in Mule 3

SUB FLOW REQUEST REPLY PROCESSOR CHAIN FOR EACH CACHE
ASYNC TCP/IP COMPOSITE SOURCE POLL UNTIL SUCCESSFUL
TRANSACTIONAL FLOW

Widely used Components in Mule 3

EXPRESSION CXF SCRIPT RUBY PYTHON
JAVASCRIPT JAVA INVOKE CUSTOM BUSINESS EVENT GROOVY
ECHO LOGGER


Widely used Transformers in Mule 3

MONGO DB XSLT TRANSFORMER REFERENCE SCRIPT RUBY
PYTHON MESSAGE PROPERTIES JAVA TRANSFORMER GZIP COMPRESS/UNCOMPRESS GROOVY
EXPRESSION DOM TO XML STRING VALIDATION COMBINE COLLECTIONS BYTE ARRAY TO STRING
ATTACHMENT TRANSFORMER FILE TO STRING XML TO DOM APPEND STRING JAVASCRIPT
JSON TO JAVA COPYBOOK TO JSON MAP TO JSON JSON TO XML FLATFILE TO JSON
FIXWIDTH TO JSON CSV TO JSON


Widely used Filters in Mule 3

WILDCARD SCHEMA VALIDATION REGEX PAYLOAD OR
NOT MESSAGE PROPERTY MESSAGE IDEMPOTENT FILTER REFERNCE
EXPRESSION EXCEPTION CUSTOM AND


Exception Strategy in Mule 3

REFERENCE EXCEPTION STRATEGY CUSTOM EXCEPTION STRATEGY CHOICE EXCEPTION STRATEGY CATCH EXCEPTION STRATEGY GLOBAL EXCEPTION STRATEGY


Flow Control in Mule 3

CHOICE COLLECTION AGGREGATOR COLLECTION SPLITTER CUSTOM AGGREGATOR FIRST SUCCESSFUL
MESSAGE CHUNK AGGREGATOR MESSAGE CHUNK SPLITTER RESEQUENCER ROUND ROBIN SOAP ROUTER