Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear browser Cookies with Selenium WebDriver Java bindings

Tags:

Does anyone know if it's possible to Clear Browser Cookies for WebDriver before starting the automation? (Note: Not Selenium RC)

like image 503
Tom J Muthirenthi Avatar asked Feb 15 '16 07:02

Tom J Muthirenthi


People also ask

How do you clear all cookies in the browser in Selenium?

Navigate to the chrome settings page with Selenium by executing the driver. get('chrome://settings/clearBrowserData') . Click on the Clear Data button to clear the cache.

Which Webdriver method will delete the cookies in Selenium?

We can delete cookies on all domains with Selenium. The method deleteAllCookies is used to delete all cookies from the present domain. First, we shall add cookies, then get them and finally delete all the cookies.

How do I delete all cookies in Java?

Start at the Tools menu and select the Internet Options. To delete all cookies, click the Delete Cookies button.

Can we manipulate cookies in Selenium?

Each cookie is associated with a name, value, domain, path, expiry, and the status of whether it is secure or not. In order to validate a client, a server parses all of these values in a cookie. When Testing a web application using selenium web driver, you may need to create, update or delete a cookie.


2 Answers

Yes, it's possible

driver.manage().deleteAllCookies(); 

Call it right after you are creating the new WebDriver instance.

WebDriver driver = new ChromeDriver(); driver.manage().deleteAllCookies(); 

You can also delete the cookies one by one

Set<Cookie> allCookies = driver.manage().getCookies(); for (Cookie cookie : allCookies) {     driver.manage().deleteCookieNamed(cookie.getName()); } 
like image 101
Guy Avatar answered Dec 21 '22 03:12

Guy


Does this work for you?

driver.manage().deleteAllCookies(); 
like image 27
Jyothishwar Deo Avatar answered Dec 21 '22 02:12

Jyothishwar Deo