selenium-wire简介

一.简介

以下来自chatGPT回答:

selenium-wire是一个基于selenium的Python库,它扩展了selenium的功能,使得我们可以在自动化测试中直接访问和修改浏览器的网络请求和响应。selenium-wire可以拦截和修改HTTP请求和响应,从而可以在测试过程中模拟 网络环境、调试和分析网络请求以及实现自定义的网络请求和响应处理逻辑。与selenium自带的webdriver不同,selenium-wire使用了第三方库mitmproxy来实现网络请求的拦截和修改。因此,使用selenium-wire需要先安装mitmproxy。

二.用法

1.安装selenium-wire库

复制代码
pip install selenium-wire

mitmproxy安装使用可参考:https://www.cnblogs.com/lihongtaoya/p/17446958.html

2.获取请求信息

1)获取所有的请求信息

get_list = driver.requests # 返回的是个数组

当调用 driver.requests时,返回的是当前页面所有已经请求并响应过了的接口数据。如果某个请求还没有完成或者被阻塞,那么这个请求对应的数据不会出现在 requests 列表中。

2)获取请求行/头/体

python 复制代码
for i in get_list:
    if 'https://www.baidu.com/sugrec' in i.url:
        print(i.url)  # 请求地址
        print(i.date)  # 请求时间
        print(i.method)  # 请求方式
        print(i.headers)  # 请求头   or i.headers['Content-Type']
        print(i.params)  # 请求参数
        print(i.host)  # 请求域名

driver.requests获取的是当前页面所有的请求,因此我们在使用时需过滤下自己所要的接口信息。

3)create_response()方法

python 复制代码
for i in get_list:
    if 'text/html' not in i.response.headers['Content-Type']:
        # create_response(status_code, headers=(), body=b'')
        i.create_response(200, [('Content-Type', 'text/plain')], b'["Hello","world"]')  # mock接口响应

create_response()为mock接口响应信息,返回我们需要的信息。

4)abort()方法

python 复制代码
for i in get_list:
    if 'text/html' not in i.response.headers['Content-Type']:
        i.abort(error_code=403)  # 中断请求,并返回状态码403
        print(i.response.status_code)

这里需要注意的是3,4方法所可以改变响应数据,但服务端记录的数据还是实际请求的值。

3.获取响应信息

这里直接贴代码吧,备注很详细。

python 复制代码
for i in get_list:
    if 'https://www.baidu.com/sugrec' in i.url:
        print(i.response.date)  # 当前响应时间
        print(i.response.reason)  # 响应状态 ok  or  Not found
        print(i.response.headers['Content-Type'])  # 响应的数据类型
        print(i.response.status_code)  # 响应状态码
 
        # 获取响应的body并转为json类型输出
        from seleniumwire.utils import decode
        body = decode(i.response.body, i.response.headers.get('Content-Encoding', 'identity'))
        decoded_response = body.decode('utf-8')  # 将二进制字节串解码为 UTF-8 编码的字符串
        json_response = json.loads(decoded_response)  # 将 JSON 字符串转换为 Python 对象
        print(json_response)

4.实例

python 复制代码
import json
import time
from selenium.webdriver.common.by import By
from seleniumwire import webdriver
 
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("https://www.baidu.com")
driver.find_element(By.ID, value='kw').send_keys('猪')
driver.find_element(By.ID, value='su').click()
time.sleep(5)  # 等待5s,让所有接口请求完
get_list = driver.requests  # 获取当前所有的请求信息
 
for i in get_list:
    if 'https://www.baidu.com/sugrec' in i.url:
        print(i.response.date)  # 当前响应时间
        print(i.response.reason)  # 响应状态 ok  or  Not found
        print(i.response.headers['Content-Type'])  # 响应的数据类型
        print(i.response.status_code)  # 响应状态码
 
        # 获取响应的body并转为json类型输出
        from seleniumwire.utils import decode
        body = decode(i.response.body, i.response.headers.get('Content-Encoding', 'identity'))
        decoded_response = body.decode('utf-8')  # 将二进制字节串解码为 UTF-8 编码的字符串
        json_response = json.loads(decoded_response)  # 将 JSON 字符串转换为 Python 对象
        print(json_response)
 
driver.quit()

selenium-wrie官方文档:https://pypi.org/project/selenium-wire/

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

相关推荐
惜.己2 分钟前
使用python读取json数据,简单的处理成元组数组
开发语言·python·测试工具·json
浮生带你学Java9 小时前
2025Java面试题及答案整理( 2025年 7 月最新版,持续更新)
java·开发语言·数据库·面试·职场和发展
墨染点香11 小时前
LeetCode Hot100【5. 最长回文子串】
算法·leetcode·职场和发展
baynk11 小时前
wireshark的常用用法
网络·测试工具·wireshark·ctf
维度软件库12 小时前
集中打印和转换Office 批量打印精灵:Word/Excel/PDF 全兼容,效率翻倍
测试工具·电脑·开源软件
im_AMBER13 小时前
Leetcode 03 java
算法·leetcode·职场和发展
轮到我狗叫了13 小时前
力扣.1312让字符串成为回文串的最少插入次数力扣.105从前序和中序遍历构造二叉树牛客.拼三角力扣.57插入区间编辑
算法·leetcode·职场和发展
木子.李34718 小时前
记录Leetcode中的报错问题
算法·leetcode·职场和发展
牛客企业服务1 天前
2025秋招突围战:AI智能监考如何重构远程笔试公平防线?
java·大数据·人工智能·面试·职场和发展·重构·求职招聘
慧都小项1 天前
降本增效!自动化UI测试平台TestComplete并行测试亮点
自动化测试·ci/cd·跨平台·ui测试·回归测试·并行测试·环境兼容