当前位置: 首页 > news >正文

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)获取请求行/头/体

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()方法

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()方法

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

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

3.获取响应信息

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

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 foundprint(i.response.headers['Content-Type'])  # 响应的数据类型print(i.response.status_code)  # 响应状态码# 获取响应的body并转为json类型输出from seleniumwire.utils import decodebody = 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.实例

import json
import time
from selenium.webdriver.common.by import By
from seleniumwire import webdriverdriver = 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 foundprint(i.response.headers['Content-Type'])  # 响应的数据类型print(i.response.status_code)  # 响应状态码# 获取响应的body并转为json类型输出from seleniumwire.utils import decodebody = 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/

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

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

http://www.lryc.cn/news/263215.html

相关文章:

  • 华为组播配置案例
  • lua语法
  • 5A-Downloader,m3u8文件转mp4文件,音视频分离ts合并、转mp4
  • 标准IO与文件IO
  • 流行的 React 相关库和框架
  • 游戏引擎?
  • C语言--字符函数与字符串函数
  • 整理了一些热门、含免费次数的api,分享给大家
  • Wireshark在网络性能调优中的应用
  • 关于设计师的自我评价(合集)
  • Hudi Clustering
  • 通过与 Team Finance 整合,Casper Network 让 Token 的创建、部署更加高效
  • Linux软件管理rpm和yum
  • uart和usart的区别
  • 原生微信小程序-使用 阿里字体图标 详解
  • 机器学习 | 机器学习基础知识
  • OpenHarmony鸿蒙原生应用开发,ArkTS、ArkUI学习踩坑学习笔记,持续更新中。
  • RHCE8 资料整理(十)二
  • CUDA 学习记录2
  • 探索Qt 6.3:了解基本知识点和新特性
  • 持续集成交付CICD:基于 GitLabCI 与 JenkinsCD 实现后端项目发布
  • 一些好用的VSCode扩展
  • 3dsmax渲染太慢,用云渲染农场多少钱?
  • JVM-9-Class类文件的结构
  • Redis持久化,性能管理
  • linux(centos7)离线安装mysql-5.7.35-1.el7.x86_64.rpm-bundle.tar
  • 【lesson17】MySQL表的基本操作--表去重、聚合函数和group by
  • 面试题总结(十二)【Qt】【华清远见西安中心】
  • GPT-4V with Emotion:A Zero-shot Benchmark forMultimodal Emotion Understanding
  • CogVLM与CogAgent:开源视觉语言模型的新里程碑