嘉兴信息网站,工程建设科学技术奖申报网站,大型网站建设规范,东莞网站建设快速排名一.简介
以下来自chatGPT回答#xff1a;
selenium-wire是一个基于selenium的Python库#xff0c;它扩展了selenium的功能#xff0c;使得我们可以在自动化测试中直接访问和修改浏览器的网络请求和响应。selenium-wire可以拦截和修改HTTP请求和响应#xff0c;从而可以在…
一.简介
以下来自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获取的是当前页面所有的请求因此我们在使用时需过滤下自己所要的接口信息。
3create_response()方法
for i in get_list:if text/html not in i.response.headers[Content-Type]:# create_response(status_code, headers(), bodyb)i.create_response(200, [(Content-Type, text/plain)], b[Hello,world]) # mock接口响应create_response()为mock接口响应信息返回我们需要的信息。
4abort()方法
for i in get_list:if text/html not in i.response.headers[Content-Type]:i.abort(error_code403) # 中断请求并返回状态码403print(i.response.status_code)这里需要注意的是34方法所可以改变响应数据但服务端记录的数据还是实际请求的值。
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, valuekw).send_keys(猪)
driver.find_element(By.ID, valuesu).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/
最后感谢每一个认真阅读我文章的人礼尚往来总是要有的虽然不是什么很值钱的东西如果你用得到的话可以直接拿走 这些资料对于【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴上万个测试工程师们走过最艰难的路程希望也能帮助到你