zoukankan      html  css  js  c++  java
  • Python3+Selenium3+webdriver学习笔记11(cookie处理)

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    '''
    Selenium3+webdriver学习笔记11(cookie处理)
    '''
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support.select import Select

    import time,os
    import random

    # about:addons 火狐浏览器安装组件,访问的地址

    # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
    #id
    keys="测试部落"
    delay=3
    url="https://www.cnblogs.com/"
    driver=webdriver.Firefox()

    driver.get(url)

    #获取cookies值-未登录
    cookies=driver.get_cookies()
    print(cookies)

    #获取cookies值-已未登
    #进入首页面-点击登录按钮-进入到登录页面
    driver.find_element_by_css_selector("span#span_userinfo>a").click()

    #输入账号、密码并点击登录按钮
    driver.find_element_by_id("input1").send_keys("用你自己的账号")
    driver.find_element_by_id("input2").send_keys("用你自己的密码")
    driver.find_element_by_id("signin").click()
    time.sleep(delay)

    #获取指定cookie
    cookie=driver.get_cookie(name='__gads')
    print(cookie)

    #清除指定的cookie
    driver.delete_cookie(name='__gads')

    cookies=driver.get_cookies()
    print(cookies)

    #清除所有cookies
    driver.delete_all_cookies()

    cookies=driver.get_cookies()
    print(cookies)


    #添加指定的cookie
    # driver.add_cookie()

    cookie1={'name': '.CNBlogsCookie',
    'value': '',
    'path': '/',
    'domain': '.cnblogs.com',
    'secure': False,
    'httpOnly': False,
    'expiry': 1606993724}

    cookie2={'name': '.Cnblogs.AspNetCore.Cookies',
    'value': '',
    'path': '/',
    'domain': '.cnblogs.com',
    'secure': False,
    'httpOnly': False,
    'expiry': 1606993724}

    driver.get("http://www.cnblogs.com/nicetime")
    driver.add_cookie(cookie1)
    driver.add_cookie(cookie2)
    time.sleep(delay)
    driver.refresh()

    cookies=driver.get_cookies()
    print(cookies)

    time.sleep(delay)
    driver.quit()


  • 相关阅读:
    动态规划算法-3
    动态规划算法-2
    动态规划算法-1
    滑动窗口算法-3
    央行副行长提示金融风险:地方偿债高峰期到来
    银行卡换“芯” 更要银行换心
    破解IT运维成本困境,专业化分工是妙方
    php连接mysql
    ajax原生验证用户名是否存在
    ajax跨域问题
  • 原文地址:https://www.cnblogs.com/NiceTime/p/10066829.html
Copyright © 2011-2022 走看看