Selenium 获取接口响应数据

目录

前言

seleniumwire简介

功能

兼容性

目录

安装

创建webdriver

获取请求

请求对象

限制请求捕获


前言

有时候需要知道UI界面操作的同时接口响应数据是否正常,这时就需要获取接口响应数据。Selenium本身没有获取接口响应的api,但是可以通过第三方库seleniumwire获取接口响应数据。

seleniumwire简介

SeleniumWire扩展了Selenium的Python绑定,使您可以访问浏览器发出的底层请求。您以与Selenium相同的方式编写代码,但您获得了额外的API来检查请求和响应,并对其进行动态更改。

功能

  • 纯Python,用户友好的API
  • 捕获HTTP和HTTPS请求
  • 拦截请求和响应
  • 动态修改标题、参数和正文内容
  • 捕获websocket消息
  • 支持HAR格式
  • 代理服务器支持

兼容性

  • Python 3.7+
  • Selenium 4.0.0+
  • Chrome, Firefox, Edge and Remote Webdriver supported

目录

安装

bash 复制代码
pip install selenium-wire

创建webdriver

python 复制代码
from seleniumwire import webdriver

注意不是从selenium包中导入。

然后像直接使用Selenium一样实例化web驱动程序。您可以传入任何所需的功能或特定于浏览器的选项,如可执行路径、无头模式等。seleniumwire也有自己的选项,可以在seleniumwire_options属性中传递。

python 复制代码
# Create the driver with no options (use defaults)
driver = webdriver.Chrome()

# Or create using browser specific options and/or seleniumwire_options options
driver = webdriver.Chrome(
    options = webdriver.ChromeOptions(...),
    seleniumwire_options={...}
)

请注意,对于webdriver的子包,您应该继续直接从selenium导入这些子包。例如,要导入WebDriverWait:

python 复制代码
# Sub-packages of webdriver must still be imported from `selenium` itself
from selenium.webdriver.support.ui import WebDriverWait

获取请求

SeleniumWire捕获浏览器发出的所有HTTP/HTTPS流量[1]。以下属性提供对请求和响应的访问权限。

driver.requests

按时间顺序捕获的请求的列表。

driver.last_request

用于检索最近捕获的请求的便利属性。这比使用driver.requests[-1]更有效。

请求对象

body

以字节为单位的请求正文。如果请求没有正文,则正文的值将为空,即b''。

headers

请求头的类似字典的对象。标头不区分大小写,允许重复。请求.headers['user-agent']将返回用户代理标头的值。如果你想替换一个标头,请确保先用del request.headers〔'header-name〕删除现有的标头,否则你会创建一个重复的标头。

response

与请求关联的响应对象。如果请求没有响应,则此选项将为"无"。

限制请求捕获

SeleniumWire的工作原理是通过后台启动的内部代理服务器重定向浏览器流量。当请求流经代理时,它们会被拦截和捕获。捕获请求可能会稍微减慢速度,但您可以做一些事情来限制捕获的内容。

driver.scopes

这接受一个正则表达式列表,这些正则表达式将与要捕获的URL相匹配。在提出任何请求之前,应该在驱动程序上设置它。当为空(默认值)时,将捕获所有URL。

python 复制代码
driver.scopes = [
    '.*stackoverflow.*',
    '.*github.*'
]

driver.get(...)  # Start making requests

# Only request URLs containing "stackoverflow" or "github" will now be captured

seleniumwire_options.exclude_hosts

排除捕获域名,不需要捕获的可以加入排除选项

使用此选项可以完全绕过Selenium Wire。对此处列出的地址发出的任何请求都将直接从浏览器发送到服务器,而不涉及SeleniumWire。请注意,如果您已经配置了上游代理,那么这些请求也将绕过该代理。

python 复制代码
options = {
    'exclude_hosts': ['host1.com', 'host2.com']  # Bypass Selenium Wire for these hosts
}
driver = webdriver.Chrome(seleniumwire_options=options)

测试代码:

python 复制代码
from seleniumwire import webdriver  # Import from seleniumwire

# Create a new instance of the Chrome driver
driver = webdriver.Chrome()

# Go to the Google home page
driver.get('https://www.google.com')

# Access requests via the `requests` attribute
for request in driver.requests:
    if request.response:
        print(
            request.url,
            request.response.status_code,
            request.response.headers['Content-Type'],
            request.response.body
        )

输出:

bash 复制代码
https://www.google.com/ 200 text/html; charset=UTF-8
https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png 200 image/png
https://consent.google.com/status?continue=https://www.google.com&pc=s&timestamp=1531511954&gl=GB 204 text/html; charset=utf-8
https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png 200 image/png
https://ssl.gstatic.com/gb/images/i2_2ec824b0.png 200 image/png
https://www.google.com/gen_204?s=webaft&t=aft&atyp=csi&ei=kgRJW7DBONKTlwTK77wQ&rt=wsrt.366,aft.58,prt.58 204 text/html; charset=UTF-8
...

其他功能可以参考

https://github.com/wkeeling/selenium-wire

相关推荐
Smoothcloud润云7 小时前
从“预测下一个词”到“预测下一个世界状态”:世界模型作为AGI新范式的深度分析报告
人工智能·测试工具·微服务·容器·github·状态模式·agi
测试199811 小时前
postman接口测试详解
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
测试秃头怪15 小时前
python&selenium自动化测试实战项目详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
简单点了17 小时前
mac安装wireshark
测试工具·macos·wireshark
西瓜程序猿2 天前
使用手机如何将纸质礼薄转换为电子礼薄?
测试工具·智能手机·创业创新·记了么·电子礼薄·份子钱
半个俗人2 天前
fiddler的基础使用
前端·测试工具·fiddler
站长工具箱2 天前
基于浏览器的键盘按键测试工具功能解析
测试工具·计算机外设
小陈的进阶之路2 天前
Selenium 滑动 vs Appium 滑动
python·selenium·测试工具·appium
程序员小远2 天前
软件测试用例总结
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
爱丽_3 天前
软件测试基础分类与核心概念整理
功能测试·测试工具·测试用例