selenium 获取cookie

# encoding: utf-8
import json
import time
from selenium import webdriver
driver_path = '/Users/mac126/chromedriver'
chromeOptions = webdriver.ChromeOptions()
#让浏览器不显示自动化测试
chromeOptions.add_argument('disable-infobars')
browser = webdriver.Chrome(executable_path=driver_path,options=chromeOptions)
# browser.get('https://login.taobao.com/member/login.jhtml?redirectURL=https%3A%2F%2Fwww.taobao.com%2F')
# # 查看本机ip,查看代理是否起作用
# # browser.get("http://httpbin.org/ip")
# time.sleep(10)
# input('请开始你的表演')
# #获取cookie
# dictCookies=browser.get_cookies()
# #json字符串编码
# jsonCookies=json.dumps(dictCookies)
# with open('cookies.json','w')as f:
#     f.write(jsonCookies)
# print('cookie加载完毕')
# #初次建立连接,随后方可修改cookie





browser.get('http://www.taobao.com')
#删除第一次连接时的cookie
browser.delete_all_cookies()
#读取登录时存储到本地cookie
with open('cookies.json','r')as f:
    listCookie=json.loads(f.read())

for cookie in listCookie:
    browser.add_cookie(
        {
            'domain':'.taobao.com',#此处xxx.com 前需要带点
            'name':cookie['name'],
            'value':cookie['value'],
            'path':'/',
            'expires':None
        }
    )
browser.get('https://www.taobao.com')

 

posted @ 2019-01-16 11:43  青春叛逆者  阅读(9156)  评论(0编辑  收藏  举报