zoukankan      html  css  js  c++  java
  • 通过UI自动化获取登录成功后的cookies存入Excel中再从Excel读取cookies实现自动登录

    一、通过driver.get_cookies()将获取到的cookies存到Excel中,最主要使用到Excel的写入xlwd和xlrd模块的函数;

    from selenium import webdriver
    import time
    import xlwt
    import os
    driver = webdriver.Chrome()
    driver.get('https://www.cnblogs.com/')
    driver.implicitly_wait(30)
    # 实战
    # 1.把cookies写到Excel中,
    # 写一次(name,value,path,domain,httpOnle,
    # secure 前面四个是cookies里面必须的值,暂时我们取这四个)
    # 思考:写那些数据,Excel怎么设计?
    # 使用什么工具写Excel---》xlwt
    # 怎么写?
    #准备Excel
    #准备workbook
    workbook = xlwt.Workbook(encoding='utf-8')
    #创建一个workbook
    worksheet = workbook.add_sheet('cookies')
    #写入excel
    #参数和值对应,Excel的表头
    worksheet.write(0,0,label = 'name')
    worksheet.write(0,1,label = 'value')
    worksheet.write(0,2,label = 'path')
    worksheet.write(0,3,label = 'domain')
    worksheet.write(0,4,label = 'httpOnly')
    worksheet.write(0,5,label = 'secure')


    #获取cookies 手动操作,获取cookies
    time.sleep(40)
    cookies = driver.get_cookies()
    for cookie in cookies:
    print(cookie)

    for i in range(1,len(cookies)+1): #1,2,3,4,5,6
    worksheet.write(i, 0, label=cookies[i-1]['name'])
    worksheet.write(i, 1, label=cookies[i-1]['value'])
    worksheet.write(i, 2, label=cookies[i-1]['path'])
    worksheet.write(i, 3, label=cookies[i-1]['domain'])
    worksheet.write(i, 4, label=cookies[i-1]['httpOnly'])
    worksheet.write(i, 5, label=cookies[i-1]['secure'])

    current_path = os.path.dirname(__file__)
    cookies_path = os.path.join(current_path,'../data/cookies.xlsx')
    workbook.save(cookies_path)
    print('Cookies save !!')


    # 二.后面执行,就只需要写Excel中
    import xlrd
    import os
    from selenium import webdriver
    import time
    #实战2
    driver = webdriver.Chrome()
    driver.get('https://www.cnblogs.com/')
    driver.implicitly_wait(30)
    #打开excel
    current_path = os.path.dirname(__file__)
    cookies_path = os.path.join(current_path,'../data/cookies.xlsx')
    workbook = xlrd.open_workbook(cookies_path)
    sheet01 = workbook.sheet_by_index(0)
    cookies_list = []
    #获取表格中的cookies并放到cookies_list里面
    for i in range(1,sheet01.nrows):
    cookie_dic={}
    cookie_dic['name'] = sheet01.cell_value(i,0)
    cookie_dic['value'] = sheet01.cell_value(i, 1)
    cookie_dic['path'] = sheet01.cell_value(i, 2)
    cookie_dic['domain'] = sheet01.cell_value(i, 3)
    cookie_dic['httpOnly'] = bool(sheet01.cell_value(i, 4))
    cookie_dic['secure'] = bool(sheet01.cell_value(i, 5))
    cookies_list.append(cookie_dic)

    #将列表中的cookies放到cookies中
    for cookie in cookies_list:
    driver.add_cookie(cookie)

    time.sleep(3)
    driver.refresh()








  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    Oracle数据库基础
    软件项目开发模式——三层模式
    JavaWeb——Ajax与MVC学习总结
    JavaWeb——EL及JSTL学习总结
    JavaWeb——过滤器及监听器
    JavaWeb——Servlet开发
  • 原文地址:https://www.cnblogs.com/tingting-yang/p/13357667.html
Copyright © 2011-2022 走看看