zoukankan      html  css  js  c++  java
  • 9. selenium+request方式的cookie绕过

    1. 首先确认POST请求的content-type类型

    2. 查看cookies数据

    3. 找到对应的headers数据

    4. 如果是application/json,传入的json数据需要时json数据格式

    post请求时:res = requests.post(url, data=data,headers=headers,verify=False)

    post请求时:res = requests.post(test_url,data=json.dumps(data),headers=headers)

    用request获取到的cookie,传给selenium使用, 全部代码示例如下: (敏感信息****表示)

    # -*- coding: utf-8 -*-
    import requests
    from selenium import webdriver
    import json
    from os.path import dirname,abspath
    
    
    def get_cookielin(driver):
        test_url = "https://login.*****.link/Account/Login"
        headers = {"Content-Type": "application/json;charset=utf-8",
                   "Accept": "application/json, application/xml, text/play, text/html, *.*",
                   "Accept-Encoding": "gzip, deflate,br",
                   "Accept-Language": "zh-CN,zh;q=0.9"}
        #根据Fiddler中content-type的类型,确认写入的data数据
        data = {"UseLoginGeetest":False,"Remember":"","Domin":"","ReturnUrl":"","UseLoginMutex":False,"MutexToken":"","LoginType":0,
                "UserName":"linyisss100349@bsasn.com","Password":
                "HADovxHy/k3PEyd9SYLvNfasasaa7APUh1/0IQ11WiDCop2WfcNAJkegRZc65W+FGojKjLDFUA5ziOhaM0IEyQ7sgfTfuhgxyEmLGF6rI1EU0pC2EmTrTOlRYYZIPXvW4tEIrrtfgFjX79AjAVmdoq08LmbZyNy5q76TTc="}
        #"UseLoginGeetest":False/Ture需要大写,Fiddler里面抓取的可能是小写
        res = requests.post(test_url,data=json.dumps(data),headers=headers)
        #data=json.dumps(data)需要传json格式的数据
        Tita_PC = res.cookies.get('Tita_PC')  #只获取‘Tita_PC’名称的cookie内容
    
        cookie1 = {'name': 'Tita_PC', 'value': Tita_PC}
        driver.get(test_url)   #写入cookie之前必须driver.get(url)
        #PhantomJS报错Unable to set Cookie,通过下面的方式修改
        driver.add_cookie(
            {
                'domain':'.italent.link',
                'name':cookie1['name'],
                'value':cookie1['value'],
                'path': '/',
                'expires': None
            }
        )
    
        driver.get(test_url)
        shishi_url = 'https://setting-cloud.*****.link/#home'
        driver.get(shishi_url)
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//*[@data-type="CloundApplication"]/span').click()
    
    def login():
        filepath = dirname(abspath(__file__))+'driver' #__file__用于获取文件的路径,abspath(__file__)获得绝对路径;dirname()用于获取上级目录
        #print(filepath)
        driver = webdriver.Chrome(filepath+'chromedriver.exe')
        driver.maximize_window()
        get_cookielin(driver)
    
    if __name__=='__main__':
        login()
    

      

    selenium中 WebDriver操作cookie的方法:

    get_cookies(): 获得所有cookie信息。

    get_cookie(name): 返回字典的key为“name”的cookie信息。

    add_cookie(cookie_dict) : 添加cookie。“cookie_dict”指字典对象,必须有name 和value 值。

    delete_cookie(name,optionsString):删除cookie信息。“name”是要删除的cookie的名称,“optionsString”是该cookie的选项,目前支持的选项包括“路径”,“域”。

    delete_all_cookies(): 删除所有cookie信息。

  • 相关阅读:
    Chrome cookies folder
    Fat URLs Client Identification
    User Login Client Identification
    Client IP Address Client Identification
    HTTP Headers Client Identification
    The Personal Touch Client Identification 个性化接触 客户识别
    购物车 cookie session
    购物车删除商品,总价变化 innerHTML = ''并没有删除节点,内容仍存在
    453
    购物车-删除单行商品-HTMLTableElement.deleteRow()
  • 原文地址:https://www.cnblogs.com/lintest/p/11728718.html
Copyright © 2011-2022 走看看