赞
踩
首先导入selenium
from selenium import webdriver
我们以携程网为例,这里我是把chrome启动放在了python的根目录下
driver = webdriver.Chrome()
driver.get("https://passport.ctrip.com/user/login")
driver.maximize_window()
时间延长20秒给我们足够的时间来输入账号和密码,然后使用print()打印cookie
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://passport.ctrip.com/user/login")
driver.maximize_window()
time.sleep(20)
cookies = driver.get_cookies()
print(cookies)
cookie是一个字典形式所以我们直接可以给cookie赋值
coo = [{'domain': 'testiot.shmsiot.top', 'httpOnly': False, 'name': 'Admin-Expires-In', 'path': '/', 'secure'
然后我们可以使用for循环不断匹配cookie值,匹配成功就停止
for cookie in coo:
driver.add_cookie(cookie)
完整代码如下,由于携程网的安全性,需要二层验证使用加后面两句定位元素代码
from selenium import webdriver import time driver = webdriver.Chrome() driver.get("https://passport.ctrip.com/user/login") driver.maximize_window() '''time.sleep(20) cookies = driver.get_cookies() print(cookies)''' # coo = [{'domain': '.ctrip.com', 'httpOnly': False, 'name': '_bfi', 'path': '/', 'secure': False, 'value': 'p1%3D100021%26p2%3D10320670296%26v1%3D2%26v2%3D1'}, {'domain': '.ctrip.com', 'expiry': 1621428453, 'httpOnly': False, 'name': '_bfs', 'path': '/', 'secure': False, 'value': '1.2'}, {'domain': '.ctrip.com', 'expiry': 1684498653, 'httpOnly': False, 'name': '_bfa', 'path': '/', 'secure': False, 'value': '1.1621426633310.2sev1k.1.1621426633310.1621426633310.1.2'}, {'domain': '.ctrip.com', 'expiry': 1652962652, 'httpOnly': False, 'name': 'IsPersonalizedLogin', 'path': '/', 'secure': False, 'value': 'T'}, {'domain': '.ctrip.com', 'expiry': 1624018651, 'httpOnly': True, 'name': 'ticket_ctrip', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'bJ9RlCHVwlu1ZjyusRi+ypZ7X2r4+yoj3FJp0szEIZ+4fCVfpIpE2Ih8FO45kaoBwwWXoMI2Qo4Ae62WXZ7PkRcHrXbQLj5/9FBZN/p014UvPkF6fxTH5c8/gYL/XSwoG4l9Z6KSzVs+D8GPHliWO6xf2IvaPZHvMkf/yWcbNhFT+es0GveXrL7smbVgBDI/8YOBuq6RB1N9WgS/xvsxSlXvXXNffBte1JFsyxgRl+3TNRcxdoiYd7Zbrd8bSN922U+M3wtEhWV6l68F4kARSHmSpcQDjJhXjlW7VTNmknE='}, {'domain': '.ctrip.com', 'expiry': 3198226652, 'httpOnly': False, 'name': 'AHeadUserInfo', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'VipGrade=10&VipGradeName=%BB%C6%BD%F0%B9%F3%B1%F6&UserName=&NoReadMessageCount=1'}, {'domain': '.ctrip.com', 'expiry': 3198226652, 'httpOnly': False, 'name': 'login_type', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.ctrip.com', 'expiry': 1624018651, 'httpOnly': False, 'name': 'DUID', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'u=AB499F898293D4213913F0904DA89A5C&v=0'}, {'domain': '.ctrip.com', 'expiry': 1624018651, 'httpOnly': False, 'name': 'IsNonUser', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'F'}, {'domain': '.ctrip.com', 'expiry': 4042022400, 'httpOnly': False, 'name': '_RDG', 'path': '/', 'secure': False, 'value': '28c12c7bc6775b25342da625036ef08da9'}, {'domain': '.ctrip.com', 'expiry': 3198226652, 'httpOnly': False, 'name': 'login_uid', 'path': '/', 'secure': False, 'value': '5CD6CA24E085D8AFB663D43B371417C4'}, {'domain': '.ctrip.com', 'expiry': 4042022400, 'httpOnly': False, 'name': '_RGUID', 'path': '/', 'secure': False, 'value': 'ae6cce13-839e-4f11-bdda-c5b47717438e'}, {'domain': '.ctrip.com', 'expiry': 1652962652, 'httpOnly': False, 'name': 'UUID', 'path': '/', 'secure': False, 'value': '9EF7B9ABE7AC44929F7A401CD31AA30E'}, {'domain': '.ctrip.com', 'expiry': 1624018651, 'httpOnly': True, 'name': 'cticket', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': '582C72022D39A86F32229B0967C747E7AA6CA7E05C4970E05FCC097B253C7D09'}, {'domain': '.ctrip.com', 'expiry': 4042022400, 'httpOnly': False, 'name': '_RSG', 'path': '/', 'secure': False, 'value': 'Hyad3EXpivAEEq5tHRnQtB'}, {'domain': '.ctrip.com', 'expiry': 4042022400, 'httpOnly': False, 'name': '_RF1', 'path': '/', 'secure': False, 'value': '183.193.169.248'}] for cookie in coo: driver.add_cookie(cookie) time.sleep(2) driver.get("https://passport.ctrip.com/user/login") driver.find_element_by_id('personpwd').send_keys('输入自己的密码') driver.find_element_by_id('personSubmit').click() time.sleep(5) driver.quit()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。