How do I disable/enable cookies in Safari?

286 views
Skip to first unread message

Slaven Slugic

unread,
May 13, 2014, 1:19:28 PM5/13/14
to seleniu...@googlegroups.com
I am trying to disable cookies in safari but I cant seem to find any information on how to do it. In Firefox you would do something like this 

  FirefoxProfile profile = new ProfilesIni().getProfile("default");

  profile.setPreference("network.cookie.cookieBehavior", 2);

 _driver = new FirefoxDriver(profile);


You would do similar in Chrome but not sure how to do it in Safari. Any help appreciated. 


Thanks


SS

          



Steve Osteen

unread,
May 14, 2014, 7:15:48 PM5/14/14
to seleniu...@googlegroups.com
My reply in Python since that's what I use:

browser = webdriver.Safari()
browser
.cleanSession = True
browser
.ensureCleanSession = True
browser
.delete_all_cookies()

You can try those, but most likely they wont work, the following is the only option that works for Safari that I could find:

def clearCache():
   
"""This will just delete the files straight from the safari folder, guarentee a clean start"""
   
try:
        os
.system("rm -rf $HOME/Library/Safari/LocalStorage/*.localstorage")
   
except Exception, e:
        log
.error("Error in clearCache: %s" %e)

David

unread,
May 15, 2014, 4:16:43 PM5/15/14
to seleniu...@googlegroups.com
Steve has good advice. You basically can't disable cookies in Safari, programmatically at least. You can clear cookies like how Steve presented, but not disable them.

One way you can disable though is by manually disabling cooking through the Safari browser UI settings/preferences configuration (if that option is available). Then start Safari driver with that browser data profile (if not the default).

Also for clearing (HTML5) local storage of the browser, you can also use javascript as mentioned in my blog post:


but for that solution, it is similar to clearing cookies, you have to be on the desired domain to clear local storage for that domain. Steve's method would delete for all domains I believe. But the javascript method scales easier for a RemoteWebDriver and Grid deployment.
Reply all
Reply to author
Forward
0 new messages