
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Send Cookies with Selenium WebDriver
We can send cookies with Selenium webdriver. A cookie is stored in a key value pair. First, we have to add cookies, then can delete them. We can also get the cookies.
Also, we have to add import org.openqa.selenium.Cookie statement for cookie implementations.
Syntax
Cookie ck = new Cookie("Automation", "QA"); driver.manage().addCookie(ck); driver.manage().getCookies(); driver.manage().deleteAllCookies();
Example
Code Implementation
import java.util.Set; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.Cookie; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class CookiesSend{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/index.htm"); // cookie set with name and value Cookie ck = new Cookie("Automation", "QA"); // cookie newly added driver.manage().addCookie(ck); // get all cookies Set<Cookie> cks = driver.manage().getCookies(); //iterate the cookies for (Cookie cook : ck) { System.out.println("Cookie name is: "+ cook.getName()); System.out.println("Cookie Value is : "+ cook.getValue()); } // delete all driver.manage().deleteAllCookies(); // get cookies Set chs = driver.manage().getCookies(); System.out.println("Cookie count after delete: "+chs.size()); driver.quit(); } }
Output
Advertisements