How can I store data into a cookie using Selenium Webdriver?

0 votes
Jun 18 in Selenium by Abhilasha
50 views

1 answer to this question.

0 votes

Hey Abhilasha, we can load a website and enter the username, password, checking remember me checkbox and submit button. The checkbox button will enable the browser cookie to store the username along with other details that we will display on the console with the help of following test on Selenium Webdriver:

package seleniumstorecookie;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class StoreCookie
{
static WebDriver driver;
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", ".//chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://easyweb.td.com/waw/idp/login.htm?execution=e1s1");
driver.findElement(By.name("login:AccessCard")).sendKeys("0000111122223333");
driver.findElement(By.name("login:Webpassword")).sendKeys("testingclass");
driver.findElement(By.name("rememberMeCBox")).click();
driver.findElement(By.id("login")).submit();
// create file named Cookie to store username Information
File file = new File("Cookie.data");
try { // Delete if any old file exists
file.delete();
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferwrite = new BufferedWriter(fileWriter);
for(Cookie cook : driver.manage().getCookies()){
String writeup = cook.getName()+";"+cook.getValue()+";"+cook.getDomain()+";"+cook.getPath()+""
+ ";"+cook.getExpiry()+";"+cook.isSecure();
bufferwrite.write(writeup);
System.out.println(writeup);
bufferwrite.newLine();
}
bufferwrite.flush();bufferwrite.close();fileWriter.close();
}catch(Exception exp){
exp.printStackTrace();
}
}
}
answered Jun 19 by Anvi
• 12,860 points

Related Questions In Selenium

0 votes
1 answer

How can I send some data to a Prompt Alert box using Selenium Webdriver?

Hey Abhishek, you can use sendKeys() method ...READ MORE

answered Jul 2 in Selenium by Abha
• 23,030 points
56 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How can I select date from a datepicker div using Selenium Webdriver?

Hey Jeevika, you can use following automation ...READ MORE

answered Jul 15 in Selenium by Abha
• 23,030 points
62 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
577 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
898 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
567 views
0 votes
1 answer
0 votes
1 answer

How can I clear the text in a text box using Selenium WebDriver?

Hello Akriti, you can clear the text ...READ MORE

answered May 29 in Selenium by Anvi
• 12,860 points
203 views