selenium cookie

http://stackoverflow.com/questions/11274908/selenium-why-my-get-cookies-method-returned-a-list-in-python


Below is my script:

# -*- coding: UTF-8 -*-
from selenium import webdriver

driver = webdriver.Firefox()

driver.get("http://www.google.com")

all_cookies = driver.get_cookies()

print all_cookies

and the print result is:

>>> 
[{u'domain': u'.google.com.hk', u'name': u'PREF', u'value': u'ID=999c3b8cf82fb5bc:U=7d4d0968915e2147:FF=2:LD=zh-CN:NW=1:TM=1341066316:LM=1341066316:S=kDqT8587qbZJj1_B', u'expiry': 1404138316, u'path': u'/', u'secure': False}, {u'domain': u'.google.com.hk', u'name': u'NID', u'value': u'61=AbRSUZokdEP3hN79nLdNOWwlF7itUX9-pmFAIBb-ysJqvoi1NBsmOa2wV7ldWgXpYBd_OsPnMxaAPiRsJyCpVbCN882MWNn6DwNm9eD6PTKU2gfDfqrj2EJr6CNVUhI6', u'expiry': 1356877516, u'path': u'/', u'secure': False}]
>>> 


Cookies contain a lot more information than simply name and value information, for example expiration date, domain, etc. Therefore, a simple key/value pair is not sufficient. If all you're interested in ONLY the name and its corresponding value, then I'd do something similar to the following to construct your own dictionary:

# -*- coding: UTF-8 -*-
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.google.com")
cookies_list = driver.get_cookies()
cookies_dict = {}
for cookie in cookies_list:
    cookies_dict[cookie['name']] = cookie['value']

print cookies_dict

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值