쿠키 자동화 처리

4362 단어 UI 자동화
import xlwt
import xlrd
from selenium import webdriver
import time
from selenium.webdriver.support.select import  Select

driver = webdriver.Chrome()
driver.get('http://127.0.0.1/zentao/my/')
driver.get('http://127.0.0.1/zentao/my-profile.html')

#—————— 、cookie    ————————————
#   cookie
# cookies = driver.get_cookies()
# for cookie in cookies:
#     print(cookie)
# #  cookie 
# cookie_value = driver.get_cookies('theme')
# print(cookie_value)


#—————— 、  cookie   ————————————
#1、     cookie
#2、      ——>  30s——>       、  、   、        ——>     cookie

#—————— 、cookie    ————————————
# #   :      
# driver.add_cookie({'domain': '127.0.0.1', 'httpOnly': False, 'name': 'windowHeight', 'path': '/zentao', 'secure': False, 'value': '577'})
# driver.add_cookie({'domain': '127.0.0.1', 'expiry': 1616159946, 'httpOnly': False, 'name': 'theme', 'path': '/zentao/', 'secure': False, 'value': 'default'})
# driver.add_cookie({'domain': '127.0.0.1', 'httpOnly': False, 'name': 'windowHeight', 'path': '/zentao/my', 'secure': False, 'value': '577'})
# driver.add_cookie({'domain': '127.0.0.1', 'expiry': 1616159946, 'httpOnly': True, 'name': 'device', 'path': '/zentao/', 'secure': False, 'value': 'desktop'})
# driver.add_cookie({'domain': '127.0.0.1', 'httpOnly': False, 'name': 'windowWidth', 'path': '/zentao', 'secure': False, 'value': '1034'})
# driver.add_cookie({'domain': '127.0.0.1', 'expiry': 1616159946, 'httpOnly': False, 'name': 'lang', 'path': '/zentao/', 'secure': False, 'value': 'zh-cn'})
# driver.add_cookie({'domain': '127.0.0.1', 'httpOnly': True, 'name': 'zentaosid', 'path': '/zentao/', 'secure': False, 'value': '3df928fecb2bf07d678d4e112fa4ebf5'})
# driver.add_cookie({'domain': '127.0.0.1', 'httpOnly': False, 'name': 'windowWidth', 'path': '/zentao/my', 'secure': False, 'value': '1034'})
#
# time.sleep(2)
# driver.refresh()#  

##   、excel +Cookie  
##  1__       cookie
# time.sleep(20) #  20s      cookie
# cookies = driver.get_cookies()
# #   xlwt
# workbook = xlwt.Workbook(encoding='utf-8')#  workbook  
# workbsheet = workbook.add_sheet('test_cookie_sheet')#     sheet
# workbsheet.write(0,0,'name')#      ,       ,      ,       
# workbsheet.write(0,1,'value')
# workbsheet.write(0,2,'path')
# workbsheet.write(0,3,'domain')
# workbsheet.write(0,4,'httpOnly')
# workbsheet.write(0,5,'secure')
# for i in range(1,len(cookies)+1):
#     workbsheet.write(i, 0, cookies[i-1]['name'])  #       ,       ,      ,       
#     workbsheet.write(i, 1, cookies[i-1]['value'])
#     workbsheet.write(i, 2, cookies[i-1]['path'])
#     workbsheet.write(i, 3, cookies[i-1]['domain'])
#     workbsheet.write(i, 4, cookies[i-1]['httpOnly'])
#     workbsheet.write(i, 5,cookies[i-1]['secure'] )
# workbook.save('20210217_cookie.xlsx')#    20210217_cookie.xlsx

##  2_    excel    
workbook = xlrd.open_workbook('20210217_cookie.xlsx')
sheet = workbook.sheet_by_name('test_cookie_sheet')
cookie_list = []
for row_num in range(1,sheet.nrows):
    cookie_dict = {}
    cookie_dict['name'] = sheet.cell_value(row_num,0)
    cookie_dict['value'] = sheet.cell_value(row_num, 1)
    cookie_dict['path'] = sheet.cell_value(row_num, 2)
    cookie_dict['domain'] = sheet.cell_value(row_num, 3)
    cookie_dict['httpOnly'] = bool(sheet.cell_value(row_num, 4))
    cookie_dict['secure'] = bool(sheet.cell_value(row_num, 5))
    cookie_list.append(cookie_dict)

for cookie in cookie_list:
    driver.add_cookie(cookie)
time.sleep(3)
driver.refresh()






좋은 웹페이지 즐겨찾기