zoukankan      html  css  js  c++  java
  • python selenium添加cookie自动登录

    1. 先用selenium手动登录,保存cookie。

    import time
    import json
    from selenium import webdriver
    
    
    options = webdriver.ChromeOptions()
    options.add_argument('--disable-gpu')  # 禁用gpu
    options.add_argument("--start-maximized")  # 窗口最大
    options.add_argument('--ignore-certificate-errors') #忽略一些莫名的问题
    # options.add_argument('--proxy-server={0}'.format(proxy.proxy))  # 加代理
    options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 开启开发者模式
    options.add_argument('--disable-blink-features=AutomationControlled')  # 谷歌88版以上防止被检测
    # options.add_argument('--headless')  # 无界面
    driver = webdriver.Chrome(options=options)  # 将chromedriver放到Python安装目录Scripts文件夹下
    driver.get('http://www.****login')
    # 此处手动输入账号密码登录网站
    time.sleep(100)
    cookies = driver.get_cookies()
    with open('cookies_180.json', 'w') as f:
        f.write(json.dumps(cookies))

    2. selenium携带cookie自动登录。

    import json
    from selenium import webdriver
    
    options = webdriver.ChromeOptions()
    options.add_argument('--disable-gpu')  # 禁用gpu
    options.add_argument("--start-maximized")  # 窗口最大
    options.add_argument('--ignore-certificate-errors')  # 忽略一些莫名的问题
    # options.add_argument('--proxy-server={0}'.format(proxy.proxy))  # 加代理
    options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 开启开发者模式
    options.add_argument('--disable-blink-features=AutomationControlled')  # 谷歌88版以上防止被检测
    # options.add_argument('--headless')  # 无界面
    driver = webdriver.Chrome(options=options)  # 将chromedriver放到Python安装目录Scripts文件夹下
    driver.get('http://***.cn')  # 此处不要再放登录的网址,可以用未登录的首页
    driver.delete_all_cookies()  # 删除所有cookie信息
    with open('cookies.json', 'r', encoding='utf-8') as f:
        cookie_list = json.loads(f.read())
    for cookie in cookie_list:
        driver.add_cookie(cookie)
    driver.refresh()
    # 到此处selenium已经自动登录了
  • 相关阅读:
    【已解决】allure空白页 报错 Uncaught TypeError: P.a.registerLanguage is not a function,Uncaught ReferenceError: allure is not defined问题解决方法
    【转】SQLServer查询死锁
    opencv-Mask(掩膜)
    opencv-cvtColor图像制式转换
    opencv-saturate_cast防溢出函数
    opencv-convertTo转换数据格式
    opencv-imwrite保存函数
    opencv-imshow显示函数
    opencv-setTo设置值
    我的蓝牙设备
  • 原文地址:https://www.cnblogs.com/loren880898/p/15107122.html
Copyright © 2011-2022 走看看