#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
Selenium3+webdriver学习笔记11(cookie处理)
'''
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select

import time,os
import random

# about:addons 火狐浏览器安装组件,访问的地址

# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
#id
keys="测试部落"
delay=3
url="https://www.cnblogs.com/"
driver=webdriver.Firefox()

driver.get(url)

#获取cookies值-未登录
cookies=driver.get_cookies()
print(cookies)

#获取cookies值-已未登
#进入首页面-点击登录按钮-进入到登录页面
driver.find_element_by_css_selector("span#span_userinfo>a").click()

#输入账号、密码并点击登录按钮
driver.find_element_by_id("input1").send_keys("用你自己的账号")
driver.find_element_by_id("input2").send_keys("用你自己的密码")
driver.find_element_by_id("signin").click()
time.sleep(delay)

#获取指定cookie
cookie=driver.get_cookie(name='__gads')
print(cookie)

#清除指定的cookie
driver.delete_cookie(name='__gads')

cookies=driver.get_cookies()
print(cookies)

#清除所有cookies
driver.delete_all_cookies()

cookies=driver.get_cookies()
print(cookies)


#添加指定的cookie
# driver.add_cookie()

cookie1={'name': '.CNBlogsCookie',
'value': '',
'path': '/',
'domain': '.cnblogs.com',
'secure': False,
'httpOnly': False,
'expiry': 1606993724}

cookie2={'name': '.Cnblogs.AspNetCore.Cookies',
'value': '',
'path': '/',
'domain': '.cnblogs.com',
'secure': False,
'httpOnly': False,
'expiry': 1606993724}

driver.get("http://www.cnblogs.com/nicetime")
driver.add_cookie(cookie1)
driver.add_cookie(cookie2)
time.sleep(delay)
driver.refresh()

cookies=driver.get_cookies()
print(cookies)

time.sleep(delay)
driver.quit()


posted on 2018-12-04 20:58  新美好时代  阅读(128)  评论(0编辑  收藏  举报