使硒捕获所有饼干 [英] Make selenium grab all cookies

查看:121
本文介绍了使硒捕获所有饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知要对我们的面向网站进行cookie审计,现在我们拥有很多域名,所以我真的不想手动挖掘每个提取cookies的人。我决定和硒一起去。这工作,直到我想抓住第三方cookie的点。

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.

目前(python)我可以做

Currently (python) I can do

driver.get_cookies()

对于从我的域设置的所有Cookie,但这不会给我任何Google ,Twitter,Vimeo或其他第三方Cookie

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

我已经尝试修改firefox驱动程序中的cookie权限,但它没有帮助。任何人都知道如何抓住tehm

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

推荐答案

您的问题已在StackOverflow 在这里

Your question has been answered on StackOverflow here

步骤1:您需要下载并安装全部Firefox中的Cookie Cookie扩展程序(来自此处(不要忘记在安装扩展程序后重新启动Firefox)。

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).

步骤2:执行此Python代码以使Selenium的FirefoxWebDriver保存所有cookie到xml文件,然后读取此文件:

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

这篇关于使硒捕获所有饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆