How to Load Cookies for login page using Selenium Scripts?

In order to load cookies, First we need to get the cookies and it should be stored on the browser.  

Please click the link to check : How to retrieve and add cookies for login activity on selenium scripts?


So that, We can avoid that logging in everytime on facebook, gmail, twitter, etc.,

Code to load Cookies:-

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Date;
import java.util.StringTokenizer;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class LoadCookieInfo {

public static void main(String... args){

//Invoke browser
WebDriver driver = new FirefoxDriver();

//Navigate to Facebook page
driver.get("http://www.facebook.com");

try{
File fl = new File("browser.data");
FileReader fr = new FileReader(fl);
BufferedReader br = new BufferedReader(fr);

String line;

while((line=br.readLine())!=null)
{
StringTokenizer str = new StringTokenizer(line,";");

while(str.hasMoreTokens()){
String name = str.nextToken();
String value = str.nextToken();
String domain = str.nextToken();
String path = str.nextToken();

Date expiry = null;

String dt;

if(!(dt=str.nextToken()).equals("null")){
expiry = new Date(dt);
}

boolean isSecure = new Boolean(str.nextToken()).
booleanValue();

Cookie ckie= new Cookie(name,value,domain,path,expiry,isSecure);
driver.manage().addCookie(ckie);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
driver.get("http://www.facebook.com");
}
}

No comments:

Post a Comment

Socialize It and Share the post with your friends
SOCIALIZE IT →
FOLLOW US →
SHARE IT →