#coding=utf-8
from selenium import webdriver
import time
import pickle
#import http.cookiejar, urllib.request
driver = webdriver.Chrome()
driver.get("http://tieba.baidu.com/")
#点击登录链接
time.sleep(2)
driver.find_element_by_link_text(u"登录").click()
time.sleep(2)
driver.find_element_by_id("TANGRAM__PSP_10__footerULoginBtn").click()
time.sleep(2)
driver.find_element_by_id("TANGRAM__PSP_10__userName").clear()
driver.find_element_by_id("TANGRAM__PSP_10__userName").send_keys('***')
driver.find_element_by_id("TANGRAM__PSP_10__password").clear()
driver.find_element_by_id("TANGRAM__PSP_10__password").send_keys('****')
driver.find_element_by_id("TANGRAM__PSP_10__submit").click()
time.sleep(3)
#截图
#driver.get_screenshot_as_file("C:\\1.png")
# 处理cookies
#获取保存cookies值
# 将cookie序列化保存下来
pickle.dump(driver.get_cookies(),open("cookies.pkl", "wb"))
#为了验证下一步使用cookies是否成功,删除所有cookies。
driver.delete_all_cookies()
#刷新浏览器后等待5秒,查看效果
driver.refresh()
time.sleep(5)
#读取cookies流
cookies = pickle.load(open("cookies.pkl", "rb"))
#构造cookies
for cookie in cookies:
cookie_dict = {
"domain": ".baidu.com", # 火狐浏览器不用填写,谷歌要需要
'name': cookie.get('name'),
'value': cookie.get('value'),
"expires": "",
'path': '/',
'httpOnly': False,
'HostOnly': False,
'Secure': False}
#添加cookies
driver.add_cookie(cookie_dict)
#再次刷新看效果
driver.refresh()
time.sleep(5)
driver.quit()
参考:http://www.jb51.net/article/128560.htm
网友评论