Nikunj Aggarwal
Nikunj Aggarwal

Reputation: 825

Not able to delete all cookies of current domain in Selenium webdriver

I am working on Selenium webDriver in which I am using the method driver.manage().deleteAllCookies(); But this method is deleting all cookies from the current domain except one. Strange!!

I am using Chrome right now.

Can anyone suggest what could be the possible cause and what we can do to delete all cookies for the current domain?

Upvotes: 3

Views: 7294

Answers (3)

Amelia Deng
Amelia Deng

Reputation: 1

I had the same issue. I wanted to log out of the system. I thought that I had deleted all cookies by calling:

driver.manage().deleteAllCookies();

After the statement finished, however, the system navigated me to the home page.

The solution for me is to navigate to log in page then delete the cookies again:

driver.get(target_url);
driver.manage().deleteAllCookies();

Upvotes: 0

Amr Lotfy
Amr Lotfy

Reputation: 2997

I had to wait all ajax operations to finish then call deleteAllCookies(), then it worked.

public void ajaxWait(long seconds) {
    try{
        while(!waitForJSandJQueryToLoad()) {
            try {
                Thread.sleep(1000);
                seconds -= 1;
                if (seconds <= 0) {
                    return;
                }
            } catch (InterruptedException var4) {
                var4.printStackTrace();
            }
        }
    } catch(Exception e){
        e.printStackTrace();
    }
}

public boolean waitForJSandJQueryToLoad() {

    WebDriverWait wait = new WebDriverWait(driver, 20);

    // wait for jQuery to load
    ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            try {
                return ((Long)((JavascriptExecutor)driver).executeScript("return jQuery.active") == 0);
            }
            catch (Exception e) {
                // no jQuery present
                e.printStackTrace();
                return true;
            }
        }
    };

    // wait for Javascript to load
    ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            return ((JavascriptExecutor)driver).executeScript("return document.readyState")
                    .toString().equals("complete");
        }
    };

    return wait.until(jQueryLoad) && wait.until(jsLoad);
}

Upvotes: 0

Tarun Lalwani
Tarun Lalwani

Reputation: 146500

driver.manage().deleteAllCookies();

This will only delete cookies on current domain. It won't delete cookies of any other domain.

So if you need to delete cookies of those domain then you need to first browse to a page from that domain and then call the deleteAllCookies method again

Upvotes: 5

Related Questions