Administrator
发布于 2026-06-17 / 1 阅读
0
0

python实现自动化获取cookie并利用其进行接口调用(selenium隐式登陆)

采用selenium实现自动化接口调用

无痕模式调用分为2种:

方案一:打开chrome并访问首页,增加options参数实现无痕模式调用

chrome_options = Options()

chrome_options.add_argument(‘–headless’)

chrome_options.add_argument(‘–disable-gpu’)

driver = webdriver.Chrome(options=chrome_options)

#方案二:实现免界面操作访问,本方案貌似已经被官方弃用了,建议改成方案一

driver = webdriver.PhantomJS()

方案一:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json

'''
采用selenium自动化显式登陆,并返回cookie
'''
def automation():
    # 方案一:打开chrome并访问首页,增加options参数实现无痕模式调用
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    driver = webdriver.Chrome(options=chrome_options)
    #方案二:实现免界面操作访问,本方案貌似已经被官方弃用了,建议改成方案一
    # driver = webdriver.PhantomJS()
    driver.get("http://192.168.32.40:8081/")
    # 获取账号、密码框的元素,再获取到登录按钮,往输入框写你的帐号密码,然后自动点击一下登录
    WAIT = WebDriverWait(driver, 3)
    username = WAIT.until(EC.presence_of_element_located((By.NAME, "username")))
    password = WAIT.until(EC.presence_of_element_located((By.NAME, "password")))
    username.send_keys('admin')
    # 此处在输入密码后按下回车键就默认判定点击了登陆按钮,然后就能实现登陆操作了,真JB骚,上面获取xpath搞死人都没验证过去,坑死
    password.send_keys('admin\n')
    cookie = driver.get_cookies()
    print(cookie)
    print(json.dumps(cookie))
    submit.click()
    return cookie
#此时的cookie是一个list集合对象
cookies =  automation()
print("cookies:",cookies)

'''
采用利用cookie进行接口调用
'''
def request_selenium_cookie():
    #将list对象转化为string对象
    cookie = [item["name"] + "=" + item["value"] for item in cookies]
    cookiestr = ';'.join(item for item in cookie)
    print("cookie:",cookie)
    print("cookiestr:", cookiestr)
    headers = {
        "Cookie": cookiestr
    }
    url = 'http://192.168.32.40:8081/webresources/ui/activity/activityList?actiName=&deptId=&status=&startDate=&endDate=&page=1&rows=10'
    try:
        response = requests.get(url,headers = headers)
        print("response code:",response.status_code)
        if response.status_code == 200 : return response.text
    except requests.RequestException:
        return None

print(request_selenium_cookie())

方案二:

import time
from selenium import webdriver

def automation_method2():
    # 方案一:打开chrome并访问首页,增加options参数实现无痕模式调用
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    #不设置options参数时则为显式登陆
    driver = webdriver.Chrome(options=chrome_options)
    # driver = webdriver.Chrome()
    driver.maximize_window()  # 最大化窗口,方便定位元素
    #方案二:实现免界面操作访问,本方案貌似已经被官方弃用了,建议改成方案一
    # driver = webdriver.PhantomJS()
    driver.get("http://192.168.32.40:8081/")
    # 获取账号、密码框的元素,再获取到登录按钮,往输入框写你的帐号密码,然后自动点击一下登录
    driver.find_element_by_name("username").send_keys('admin')
    driver.find_element_by_name("password").send_keys('admin')
    #休眠4秒
    time.sleep(4)
    driver.find_element_by_name("password").send_keys(Keys.ENTER)
    cookie = driver.get_cookies()
    print(cookie)
    print(json.dumps(cookie))
    return cookie
#此时的cookie是一个list集合对象
method2_cookies =  automation_method2()
print("automation_method2 cookies:",method2_cookies)

def request_selenium_cookie_method2():
    #将list对象转化为string对象
    cookie = [item["name"] + "=" + item["value"] for item in method2_cookies]
    cookiestr = ';'.join(item for item in cookie)
    print("cookie:",cookie)
    print("cookiestr:", cookiestr)
    headers = {
        "Cookie": cookiestr
    }
    url = 'http://192.168.32.40:8081/webresources/ui/activity/activityList?actiName=&deptId=&status=&startDate=&endDate=&page=1&rows=10'
    try:
        response = requests.get(url,headers = headers)
        print("response code:",response.status_code)
        if response.status_code == 200 : return response.text
    except requests.RequestException:
        return None

print(request_selenium_cookie_method2())

评论