一、验证码由来:
#encoding=utf-8
import time
from selenium import webdriver
driver1 = webdriver.Chrome(executable_path=r'../conf/chromedriver.exe')
driver1.maximize_window()
driver1.get('http://XXXXXXXXXX/login')
#一旦加载网站,即使没登录,也会产生一个cookie,需要删除cookie
driver1.delete_all_cookies()
cookie = {"SESSION":"这里填写session值;",
"其他cookie键值对":"其他cookie键值对;",
"其他cookie键值对":"其他cookie键值对;",
"其他cookie键值对":"其他cookie键值对",
"其他cookie键值对":"其他cookie键值对",
"JSESSIONID":"这里填写jsessionid的值"
}
driver1.add_cookie({"domain": "域名", "name": "SESSION", "value": "session值",
"expires": "Session", "path": "path是什么就填写什么", "httpOnly": False, "HostOnly": False,
"Secure": False})
driver1.add_cookie({"domain": "域名", "name": "其他cookie键值对", "value": "其他cookie键值对",
"expires": "Session", "path": "path是什么就填写什么", "httpOnly": False, "HostOnly": False,
"Secure": False})
driver1.add_cookie({"domain": "域名", "name": "其他cookie键值对", "value": "其他cookie键值对",
"expires": "Session", "path": "path是什么就填写什么", "httpOnly": False, "HostOnly": False,
"Secure": False})
driver1.add_cookie({"domain": "域名", "name": "其他cookie键值对", "value": "其他cookie键值对",
"expires": "Session", "path": "path是什么就填写什么", "httpOnly": False, "HostOnly": False,
"Secure": False})
driver1.add_cookie({"domain": "域名", "name": "JSESSIONID", "value": "这里填写jsessionid的值",
"expires": "Session", "path": "path是什么就填写什么", "httpOnly": False, "HostOnly": False,
"Secure": False})
driver1.add_cookie({"domain": "域名", "name": "其他cookie键值对", "value": "其他cookie键值对",
"expires": "Session", "path": "path是什么就填写什么", "httpOnly": False, "HostOnly": False,
"Secure": False})
time.sleep(2)
#列表里面有两个字典cookie信息,一个是登录之前的,一个是登录之后的
save_cookie = driver1.get_cookies()
print(save_cookie)
driver1.quit()
driver2 = webdriver.Chrome(executable_path=r'../conf/chromedriver.exe')
driver2.maximize_window()
# 必须首先加载网站,这样selenium才知道cookie是属于哪个网站的
driver2.get('http://XXXXXXXXXX/login')
print(driver2.get_cookies())
#一旦加载网站,即使没登录,也会产生一个cookie,需要删除cookie
driver2.delete_all_cookies()
for cookie in save_cookie:
# 添加driver1登录成功之后的cookie
driver2.add_cookie(cookie)
driver2.get('http://XXXXXXXXXX/index')
print(driver2.get_cookies())
time.sleep(3)
方法二:
#encoding=utf-8
import time
from selenium import webdriver
def autoSessionLoginSuccess():
driver = webdriver.Chrome(
executable_path=r'XXXXXXXXXX\conf\chromedriver.exe')
driver.maximize_window()
driver.get('XXXXXXXXXX/login')
driver.delete_all_cookies()
cookie = {
"SESSION": "XXXXXXXXXX;",
"XXXXXXXXXX": "login;",
"XXXXXXXXXX": "login;",
"XXXXXXXXXX": "login",
"XXXXXXXXXX": "login",
"JSESSIONID": "XXXXXXXXXX"
}
driver.add_cookie({"domain": "XXXXXXXXXX", "name": "SESSION", "value": "XXXXXXXXXX",
"expires": "Session", "path": "XXXXXXXXXX", "httpOnly": False, "HostOnly": False,
"Secure": False})
driver.add_cookie({"domain": "XXXXXXXXXX", "name": "XXXXXXXXXX", "value": "login",
"expires": "Session", "path": "XXXXXXXXXX", "httpOnly": False, "HostOnly": False,
"Secure": False})
driver.add_cookie({"domain": "XXXXXXXXXX", "name": "XXXXXXXXXX", "value": "login",
"expires": "Session", "path": "XXXXXXXXXX", "httpOnly": False, "HostOnly": False,
"Secure": False})
driver.add_cookie({"domain": "XXXXXXXXXX", "name": "XXXXXXXXXX", "value": "login",
"expires": "Session", "path": "XXXXXXXXXX", "httpOnly": False, "HostOnly": False,
"Secure": False})
driver.add_cookie({"domain": "XXXXXXXXXX", "name": "JSESSIONID", "value": "XXXXXXXXXX",
"expires": "Session", "path": "XXXXXXXXXX", "httpOnly": False, "HostOnly": False,
"Secure": False})
driver.add_cookie({"domain": "XXXXXXXXXX", "name": "XXXXXXXXXX", "value": "login",
"expires": "Session", "path": "XXXXXXXXXX", "httpOnly": False, "HostOnly": False,
"Secure": False})
time.sleep(2)
driver.refresh()
driver.get('XXXXXXXXXX/index')
return driver
if __name__ == '__main__':
#driver1 = webdriver.Chrome(executable_path="../conf/chromedriver.exe")
autoSessionLoginSuccess()
4.注意:需要访问两次地址,第一访问登录地址,然后手动加载上cookie信息,第二次访问地址再带上登录的cookie,就可以登录成功!
第一次访问、第二次访问都需要删除网站自带的cookie,不然是登录不成功的,我一开始只删除了第二次访问地址的cookie信息,怎么也登录不成功,费了好长时间,最后发现多一个session信息,然后发现是因为第一次登录没有删除网站自带的session,这样第二次访问时就有两个session了,就会登录失败!
5.查看cookie信息,F12就可以了!
四、其他方法,研究好了再补充进来!