4

I was told to do a cookie audit of our front facing sites, now we own alot of domains, so Im really not going to manually dig through each one extracting the cookies. I decided to go with selenium. This works up till the point where I want to grab third party cookies.

Currently (python) I can do

driver.get_cookies()

For all the cookies that are set from my domain, but this doesn't give me any Google, Twitter, Vimeo, or other 3rd party cookies

I have tried modifying the cookie permissions in the firefox driver, but it doesn't help. Anyone know how I can get hold of tehm

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91

4 Answers4

3

Your question has been answered on StackOverflow here

Step 1: You need to download and install "Get All Cookies in XML" extension for Firefox from here (don't forget to restart Firefox after installing the extension).

Step2: Execute this python code to have Selenium's FirefoxWebDriver save all cookies to an xml file and then read this file:

from xml.dom import minidom
from selenium import webdriver
import os
import time


def determine_default_profile_dir():
    """
    Returns path of Firefox's default profile directory

    @return: directory_path
    """
    appdata_location = os.getenv('APPDATA')
    profiles_path = appdata_location + "/Mozilla/Firefox/Profiles/"
    dirs_files_list = os.listdir(profiles_path)
    default_profile_dir = ""
    for item_name in dirs_files_list:
        if item_name.endswith(".default"):
            default_profile_dir = profiles_path + item_name
    if not default_profile_dir:
        assert ("did not find Firefox default profile directory")

    return default_profile_dir


#load firefox with the default profile, so that the "Get All Cookies in XML" addon is enabled
default_firefox_profile = webdriver.FirefoxProfile(determine_default_profile_dir())
driver = webdriver.Firefox(default_firefox_profile)


#trigger Firefox to save value of all cookies into an xml file in Firefox profile directory
driver.get("chrome://getallcookies/content/getAllCookies.xul")
#wait for a bit to give Firefox time to write all the cookies to the file
time.sleep(40)

#cookies file will not be saved into directory with default profile, but into a temp directory.
current_profile_dir = driver.profile.profile_dir
cookie_file_path = current_profile_dir+"/cookie.xml"
print "Reading cookie data from cookie file: "+cookie_file_path

#load cookies file and do what you need with it
cookie_file = open(cookie_file_path,'r')
xmldoc = minidom.parse(cookie_file)

cookie_file.close()
driver.close()

#process all cookies in xmldoc object
Community
  • 1
  • 1
Max Zalota
  • 56
  • 5
2

Selenium can only get the cookies of the current domain:

getCookies

java.util.Set getCookies()

Get all the cookies for the current domain. This is the equivalent of calling "document.cookie" and parsing the result

Anyway, I heard somebody used a Firefox plugin that was able to save all the cookies in XML. As far as I know, it is your best option.

Erki M.
  • 5,022
  • 1
  • 48
  • 74
0

Yes, I don't believe Selenium allows you to interact with cookies other than your current domain.

If you know the domains in question, then you could navigate to that domain but I assume this is unlikely.

It would be a massive security risk if you could access cookies cross site

Robbie Wareham
  • 3,380
  • 1
  • 20
  • 38
-1

You can get any cookie out of the browsers sqlite database file in the profile folder. I added a more complete answer here: Selenium 2 get all cookies on domain

fire
  • 149
  • 1
  • 5