python+selenium获取与使用百度登录cookie
2021-03-01 10:27
标签:win print pre rgba size pen pass word 一个 python+selenium获取与使用百度登录cookie 标签:win print pre rgba size pen pass word 一个 原文地址:https://www.cnblogs.com/yang0803/p/14446417.html
from selenium import webdriver
import time
import json
wb = webdriver.Chrome()
wb.get(‘https://www.baidu.com‘)
wb.maximize_window()
time.sleep(3)
# 登录链接
url = wb.find_element_by_xpath(‘//*[@id="u1"]/a‘).click()
# 切换为用户名密码登录
time.sleep(2)
wb.find_element_by_xpath(‘//*[@id="TANGRAM__PSP_11__footerULoginBtn"]‘).click()
wb.find_element_by_name(‘userName‘).send_keys(‘******‘)
wb.find_element_by_name(‘password‘).send_keys(‘******‘)
wb.find_element_by_id(‘TANGRAM__PSP_11__submit‘).click()
time.sleep(50)# 留出时间用于处理验证码等
# 抓取cookie
dictCookies = wb.get_cookies()
jsonCookies = json.dumps(dictCookies)
with open(‘D:\\baiducookies.txt‘, ‘w‘) as f:
f.write(jsonCookies)
print(‘cookies保存成功!‘)
wb.quit()
# 再打开一个浏览器测试cookie是否成功
wb2 = webdriver.Chrome()
wb2.get(‘https://www.baidu.com‘)
wb2.maximize_window()
time.sleep(3)
# 打开保存的cookie文件
with open(‘D:\\baiducookies.txt‘, ‘r‘, encoding=‘utf8‘) as f:
listCookies = json.loads(f.read())
for cookie in listCookies:
wb2.add_cookie(cookie)
# 读取完cookie刷新页面
wb2.refresh()
文章标题:python+selenium获取与使用百度登录cookie
文章链接:http://soscw.com/index.php/essay/58511.html