selenium-wire简介_seleniumwire(1)

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

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) # 中断请求,并返回状态码403
print(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 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.实例



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/


**最后:**下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取**【保证100%免费】**


![在这里插入图片描述](https://img-blog.csdnimg.cn/9d205f76a3bb49d8bce29972548618fa.png#pic_center)


#### **软件测试面试文档**




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618635766)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 16
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值