Issue 5212 in selenium: Safari driver - clear cookies not working

124 views
Skip to first unread message

sele...@googlecode.com

unread,
Feb 21, 2013, 11:02:06 AM2/21/13
to selenium-develope...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium Status-Untriaged

New issue 5212 by turturie...@gmail.com: Safari driver - clear cookies not
working
http://code.google.com/p/selenium/issues/detail?id=5212

Clear cookies doesn't work on safariDriver

What steps will reproduce the problem?
1.Do a login to any web site
2.Call "driver.manage().deleteAllCookies()"
3.navigate to one page that requieres the login cookie

What is the expected output? What do you see instead?
The page should not keep the cookie and the user should not be loggein

Selenium version: 2.30.0
OS: mac 10.8.2
Browser: safari
Browser version: 6.02


--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

sele...@googlecode.com

unread,
Feb 24, 2013, 4:01:35 PM2/24/13
to selenium-develope...@googlegroups.com
Updates:
Labels: Component-WebDriver Browser-Safari

Comment #1 on issue 5212 by barancev: Safari driver - clear cookies not
working
http://code.google.com/p/selenium/issues/detail?id=5212

(No comment was entered for this change.)

sele...@googlecode.com

unread,
Mar 21, 2013, 6:29:01 AM3/21/13
to selenium-develope...@googlegroups.com

Comment #2 on issue 5212 by blazergr...@gmail.com: Safari driver - clear
I am also facing the same problem. Unable to clear the cookies through
selenium web driver...

sele...@googlecode.com

unread,
Apr 8, 2013, 7:43:54 PM4/8/13
to selenium-develope...@googlegroups.com

Comment #3 on issue 5212 by jackw...@gmail.com: Safari driver - clear
I am also having the same problem. I need this because of how Safari is
setup. The weird part here is that getCookies(_) works and returns 17 to
the Set but the delete method makes no change.

Selenium Version: 2.31.0
OS X, 10.8.3
Browser: 6.0.3

Repro steps are the same as above. Seems like this would be a higher
priority. Its impossible to run multiple tests if you have to manually
reset Safari between each test.

sele...@googlecode.com

unread,
May 7, 2013, 9:32:24 PM5/7/13
to selenium-develope...@googlegroups.com

Comment #4 on issue 5212 by manga...@gmail.com: Safari driver - clear
I think there is a workaround to this - you're welcome to try it. But
first, so this issue only affects Safari and works fine for all other
browsers?

Bear in mind that deleteAllCookies() may still behave same as the Selenium
RC version: deleteAllVisibleCookies(). So you have to be sure you can see
those cookies before you delete them (can check via Selenium get cookie
calls or using javascript "document.cookie" in browser javascript/developer
console. if get cookie back then it is visible, else not).

The workaround is to go to page where the cookie is checked/invoked (e.g.
at the login / my account page that checks if logged in via cookie and
either shows page or redirects back to login page based on cookie check.
Then forcefully delete that cookie via deleteCookie() or use
deleteAllCookies(). Then do a forced page refresh. If you were on the my
accounts page (already logged in), deleting cookie followed by page refresh
should then take you back to login page since your cookie not exist
anymore. Use this workaround as a test "precondition" to execute before
proceeding with test flow that is expecting cookie not to exist. So to
reiterate the workaround: follow the reproduce steps of this bug but swap
steps 3 & 2, then add step 4 - page refresh. Now can proceed with test as
if no bug occurred.

I've ran into this issue for Safari and IE (since both don't offer
a "clean" profile, and deleteAllCookies() is not functioning good enough)
and that was my workaround which has worked well for me.

sele...@googlecode.com

unread,
May 7, 2013, 9:34:45 PM5/7/13
to selenium-develope...@googlegroups.com

Comment #5 on issue 5212 by manga...@gmail.com: Safari driver - clear
Forgot to mention, the workaround has been working for my team where we do
run Safari with multiple instances in parallel on same machine as well as
multiple tests being run one after another. But of course we use Safari 5
on Mac with Selenium v2.28, but that might not make much of a difference
from the reported environment for this bug.

sele...@googlecode.com

unread,
May 19, 2013, 1:09:35 PM5/19/13
to selenium-develope...@googlegroups.com
Updates:
Cc: jmle...@gmail.com
Labels: -Status-Untriaged

Comment #6 on issue 5212 by jmle...@gmail.com: Safari driver - clear
The SafariDriver doesn't have access to the browser's cookie jar, so it has
to delete cookies through javascript (i.e. manipulating document.cookie).
This means any HttpOnly cookie, like many login cookies, cannot be modified
by the driver.

For deleteAllCookies, we can probably just nuke the cookie file on disk
(although that may require a browser restart, which would be a non-starter
for us)

sele...@googlecode.com

unread,
Aug 27, 2013, 8:30:56 AM8/27/13
to selenium-develope...@googlegroups.com

Comment #7 on issue 5212 by nagaraz...@gmail.com: Safari driver - clear
for IE you can set the below capability as true:
ie.ensureCleanSession

for Safari:
safari.cleanSession as true

I tested in IE, it works.
For safari it does not.

http://code.google.com/p/selenium/wiki/DesiredCapabilities

sele...@googlecode.com

unread,
Feb 1, 2014, 5:51:13 PM2/1/14
to selenium-develope...@googlegroups.com

Comment #8 on issue 5212 by jkand...@gmail.com: Safari driver - clear
I did face this issue and resolved it by starting with a clean session.

This option essentially removes cookies, cache, local storage etc.

DesiredCapabilities desiredCapabilities = DesiredCapabilities.safari();
SafariOptions safariOptions = new SafariOptions();
safariOptions.setUseCleanSession(true);
desiredCapabilities.setCapability(SafariOptions.CAPABILITY, safariOptions);

sele...@googlecode.com

unread,
Feb 25, 2014, 8:01:26 PM2/25/14
to selenium-develope...@googlegroups.com

Comment #9 on issue 5212 by sant...@gmail.com: Safari driver - clear
Hi, Are you sure that you have resolved the issue by starting with a clean
session?

I tried using the desired capability mentioned below:
DesiredCapabilities desiredCapabilities = DesiredCapabilities.safari();
SafariOptions safariOptions = new SafariOptions();
safariOptions.setUseCleanSession(true);
desiredCapabilities.setCapability(SafariOptions.CAPABILITY, safariOptions);

But, unfortunately, it couldn't clear cookies.

sele...@googlecode.com

unread,
Feb 25, 2014, 11:51:20 PM2/25/14
to selenium-develope...@googlegroups.com
Updates:
Cc: -jmle...@gmail.com

Comment #10 on issue 5212 by jmle...@gmail.com: Safari driver - clear
(No comment was entered for this change.)

sele...@googlecode.com

unread,
Mar 13, 2014, 12:35:07 PM3/13/14
to selenium-develope...@googlegroups.com

Comment #11 on issue 5212 by abod...@gmail.com: Safari driver - clear
Same here - running on safari 7.0.2 (MacOS), selenium server 2.32 (can't
use anything higher because it's broken totally due to #7091).

Cookies are not cleared, session cookies persist, I was able to confirm
that using developer console - resources tab.

sele...@googlecode.com

unread,
Mar 24, 2014, 4:32:13 PM3/24/14
to selenium-develope...@googlegroups.com

Comment #12 on issue 5212 by gurb...@assay-ingenieria.com: Safari driver -
I resolve this issue deleting the files directly from the
folders "LocalStorage" and "Cookies" recursively. You can find this folders
on: (Windows)

C:\Users\%USER_NAME%\AppData\Roaming\Apple Computer\Safari\Cookies
C:\Users\%USER_NAME%\AppData\Local\Apple Computer\Safari\LocalStorage

The code looks like

File cookies = new File("C:\\Users\\%USER_NAME%\\AppData\\Roaming\\Apple
Computer\\Safari\\Cookies");
FileUtils.cleanDirectory(cookies); <-you need create this method.
cookies = new File("C:\\Users\\Admin\\AppData\\Local\\Apple
Computer\\Safari\\LocalStorage");
FileUtils.cleanDirectory(cookies); <-you need create this method.

sele...@googlecode.com

unread,
Mar 24, 2014, 4:36:46 PM3/24/14
to selenium-develope...@googlegroups.com

Comment #13 on issue 5212 by gurb...@assay-ingenieria.com: Safari driver -
I solved this issue deleting the files directly from the
folders "LocalStorage" and "Cookies" recursively. You can find these
folders like: (Windows)

C:\Users\%USER_NAME%\AppData\Roaming\Apple Computer\Safari\Cookies
C:\Users\%USER_NAME%\AppData\Local\Apple Computer\Safari\LocalStorage

sele...@googlecode.com

unread,
Apr 17, 2014, 3:54:25 AM4/17/14
to selenium-develope...@googlegroups.com

Comment #14 on issue 5212 by nhu.tran...@googlemail.com: Safari driver -
I just tried this out and it works
public void deleteCookies() {
Set<Cookie> cookies = driver.manage().getCookies();
driver.manage().deleteAllCookies();
if (!cookies.isEmpty()) {
System.out.println(cookies.size());
System.out.println(cookies.toString());
Iterator<Cookie> iter= driver.manage().getCookies().iterator();
while(iter.hasNext()){
Cookie C = iter.next();
System.out.println(C.getName()+"\n" + C.getPath()+"\n"+
C.getDomain()+"\n"+C.getValue()+"\n"+C.getExpiry());
}
cookies.clear();
System.out.println(cookies.size());
System.out.println(cookies.toString());

sele...@googlecode.com

unread,
Apr 23, 2014, 10:50:49 AM4/23/14
to selenium-develope...@googlegroups.com

Comment #15 on issue 5212 by nhu.tran...@googlemail.com: Safari driver -
Sorry, i have commended the wrong code
driver.manage().deleteAllCookies();
// delete cookies in safari
if (!driver.manage().getCookies().isEmpty()) {
Set<Cookie> cookies = driver.manage().getCookies();
Iterator<Cookie> iter= cookies.iterator();.....

sele...@googlecode.com

unread,
Jan 19, 2015, 8:57:28 PM1/19/15
to selenium-develope...@googlegroups.com

Comment #16 on issue 5212 by taylorta...@gmail.com: Safari driver - clear
cookies not working
https://code.google.com/p/selenium/issues/detail?id=5212

Above method deleteCookies() doesn't work for me. Any other suggestions?

sele...@googlecode.com

unread,
Feb 17, 2015, 3:02:40 AM2/17/15
to selenium-develope...@googlegroups.com

Comment #17 on issue 5212 by michael....@gmail.com: Safari driver - clear
Same problem here. deleteAllCookies() does not clear (at least session-)
cookies.
driver.manage().getCookies() does not return any results.

We call this in our "@Before" test-method, so that all single tests have a
clean system (no login). But on Safari the login persists and tests fail.

sele...@googlecode.com

unread,
Feb 17, 2015, 3:05:52 AM2/17/15
to selenium-develope...@googlegroups.com

Comment #18 on issue 5212 by michael....@gmail.com: Safari driver - clear
Selenium 2.44
WebDriver 2.38
Safari 8.0.3
OS X 10.10.2

sele...@googlecode.com

unread,
Feb 17, 2015, 6:50:39 PM2/17/15
to selenium-develope...@googlegroups.com

Comment #19 on issue 5212 by manga...@gmail.com: Safari driver - clear
On comment 17, have you tried navigating to the page/site where the session
cookies would take effect and then get cookies there and delete them there?
Can make that as part of @Before. It may not work if you just blindly call
deleteAllCookies() or getCookies() w/o being on the actual site/page that
uses the cookies. It's not the same as how a browser works for cookie
management where you can be on any page to manage cookie store/cache. I
mentioned that in an earlier comment.

sele...@googlecode.com

unread,
Apr 8, 2015, 10:41:27 AM4/8/15
to selenium-develope...@googlegroups.com

Comment #20 on issue 5212 by Brian.Mi...@gmail.com: Safari driver - clear
I too am having this problem:

Env:
OSX 10.10.2
Selenium 2.45
Ruby 2.0.0-p643
Capybara 2.4.4

Error:
Timed out awaiting response to command "deleteAllCookies" after 30007 ms
(Selenium::WebDriver::Error::ScriptTimeOutError)

/Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/selenium-webdriver-2.45.0/lib/selenium/webdriver/safari/bridge.rb:73:in
`raw_execute'

/Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/selenium-webdriver-2.45.0/lib/selenium/webdriver/remote/bridge.rb:618:in
`execute'

/Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/selenium-webdriver-2.45.0/lib/selenium/webdriver/remote/bridge.rb:367:in
`deleteAllCookies'

/Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/selenium-webdriver-2.45.0/lib/selenium/webdriver/common/options.rb:67:in
`delete_all_cookies'

/Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara/selenium/driver.rb:95:in
`reset!'

/Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara/session.rb:103:in
`reset!'

/Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara.rb:257:in
`block in reset_sessions!'

/Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara.rb:257:in
`each'

/Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara.rb:257:in
`reset_sessions!'

/Users/jenkins/.rvm/gems/ruby-2.0.0-p643/gems/capybara-2.4.4/lib/capybara/cucumber.rb:8:in
`After'

driver setup:

safari_opts = Selenium::WebDriver::Safari::Options.new
safari_opts.skip_extension_installation = true
# convert to capabilites
safari_caps = safari_opts.to_capabilities
# adjust clean session flag from nil to true
safari_caps['safari.options']['cleanSession'] = true
Capybara::Selenium::Driver.new(app, browser: suffix,
desired_capabilities: safari_caps)


Unfortunately the site i'm testing on is internal, but i'm confident that
this problem would exist on any site where login information is saved in a
cookie.

sele...@googlecode.com

unread,
Jun 4, 2015, 9:11:53 AM6/4/15
to selenium-develope...@googlegroups.com

Comment #21 on issue 5212 by johan.dj...@gmail.com: Safari driver - clear
I've successfully used this method to expire login cookies rather than
deleting them:
domain = "." + $base_url.gsub("https://", "")
cookie = "document.cookie = 'svssid=; domain=#{domain}; path=/;
expires=Thu, 01 Jan 1970 00:00:01 GMT;'"
execute_script(cookie)

sele...@googlecode.com

unread,
Jul 9, 2015, 10:46:50 AM7/9/15
to selenium-develope...@googlegroups.com

Comment #22 on issue 5212 by vincebow...@gmail.com: Safari driver - clear
This is a java method I've successfully used to expire each cookie one by
one; I call it from the @Before method to reset the browser, and it works
even before any page has been loaded.

public void deleteAllCookiesOneByOne() {
int noOfCookies = driver.manage().getCookies().size();
if (noOfCookies > 0) {
System.out.println("Number of cookies found: " +
Integer.toString(noOfCookies));
}
Set<Cookie> cookies = driver.manage().getCookies();
for (Cookie cookie: cookies) {
System.out.println("Cookie found with name: " +
cookie.getName() + " and path: " + cookie.getPath() + " and domain: " +
cookie.getDomain());
String javascriptCall = "document.cookie = \"" +
cookie.getName() + "=; path=" + cookie.getPath() + "; expires=Thu,
01-Jan-1970 00:00:01 GMT;\"";
System.out.println("Attempting to expire the cookie with the
following script: " + javascriptCall);
((JavascriptExecutor)driver).executeScript(javascriptCall);
}
System.out.println("Number of cookies is now: " +
Integer.toString(driver.manage().getCookies().size()));
}

My console output shows it doing the job:

Number of cookies found: 1
Cookie found with name: .ASPXAUTH and path: / and domain: null
Attempting to expire the cookie with the following script:
document.cookie = ".ASPXAUTH=; path=/; expires=Thu, 01-Jan-1970 00:00:01
GMT;"
Number of cookies is now: 0

This was inspired by, and adapted from johan's ruby code in #21, around the
structure suggested by nhu in #14.

sele...@googlecode.com

unread,
Aug 19, 2015, 6:14:29 PM8/19/15
to selenium-develope...@googlegroups.com

Comment #23 on issue 5212 by santus...@gmail.com: Safari driver - clear
Hi Everyone,

Finally I was able to find a workaround for Safari clean session issue. I
execute the following lines of code when ever I need clean session.

String[] args =
{"/usr/bin/osascript", "/Users/automation/Documents/ClearCacheScript3.scpt"};

try {
Process process = Runtime.getRuntime().exec(args);
process.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Here ClearCacheScript3.scpt is the applescript used to clear the browser
data. Following is the scripts:

tell application "Safari" to activate
delay 2
tell application "System Events" to tell process "Safari"
keystroke "," using command down
tell window 1
delay 3
click button "Privacy" of toolbar 1
delay 3
click button 2 of group 1 of group 1
delay 1
click button "Remove Now" of sheet 1

end tell
keystroke "w" using command down
end tell

Created the above script based on the one in Link 1 below. I have modified
it to clear the data for all the websites. If you need to clear data for
specific website, refer to the below link. But it is not working around
the "Details..." button, so you might have to modify it to :
click button "Details..." of group 1 of group 1


Link 1: https://discussions.apple.com/thread/4011487?tstart=0

Attachments:
Preferences.tiff 95.4 KB
ClearCacheScript3.scpt 3.2 KB

sele...@googlecode.com

unread,
Jan 29, 2016, 11:45:09 AM1/29/16
to selenium-develope...@googlegroups.com
Updates:
Status: Duplicate

Comment #25 on issue 5212 by luke.semerau: Safari driver - clear cookies
not working
https://code.google.com/p/selenium/issues/detail?id=5212

https://github.com/SeleniumHQ/selenium/issues/1196
Reply all
Reply to author
Forward
0 new messages