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()