selenium自动化 | 通过获取cookies登录

》》》登录百度《《《
#
获取登录成功后的cookies def get_cookies(): driver.get(bd_url) driver.implicitly_wait(10)#隐式等待 #登录操作 ActionChains(driver)\ .move_to_element(driver.find_element_by_link_text('登录'))\ .click()\ .perform() ActionChains(driver)\ .move_to_element(driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__footerULoginBtn"]'))\ .click()\ .perform() driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__userName"]').send_keys('name') driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__password"]').send_keys('passwd') driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__submit"]').click() time.sleep(25)#等待期间进行滑动、输入验证码工作 #获取登录成功之后的cookies cookies = driver.get_cookies() #保存cookies到本地 with open('./bd_login_cookies.txt','w')as f:f.write(str(cookies)) driver.quit() return cookies if __name__ == '__main__': #重新打开网站,无需更多操作即可登录 # get_cookies()#获取登录所需cookies driver.delete_all_cookies()#删除cookies driver.get(bd_url)#访问 cookies=eval(open('./bd_login_cookies.txt','r').read())# print(cookies) for cookie in cookies: if 'expiry' in cookie: del cookie['expiry'] driver.add_cookie(cookie) driver.refresh()

 


》》》登录博客园《《《
from selenium import webdriver
driver=webdriver.Chrome()
driver.delete_all_cookies()
driver.get('https://account.cnblogs.com/signin?returnUrl=https%3A%2F%2Fwww.cnblogs.com%2F')
#登录操作
driver.find_element_by_xpath('//*[@id="LoginName"]').send_keys('name')
driver.find_element_by_xpath('//*[@id="Password"]').send_keys('passwd')
driver.find_element_by_xpath('//*[@id="submitBtn"]').click()
time.sleep(5)#等待期间进行滑动工作
#获取登录成功之后的cookies
cookies = driver.get_cookies()
for cookie in cookies:driver.add_cookie(cookie)#向driver添加cookies
print(cookies)
#访问上传文件页面
driver.get('https://i.cnblogs.com/files')
driver.find_element_by_xpath('/html/body/cnb-root/cnb-layout/div[2]/div[3]/div[2]/div/cnb-spinner/div/cnb-files/cnb-collapse-panel[1]/div[2]/cnb-spinner/div/div[2]/cnb-form-field/div/div/input')

 

posted @ 2020-05-11 17:30  人人都爱小雀斑  阅读(622)  评论(0编辑  收藏  举报