Selenium中如何抓取网络请求响应及WebSocket信息

📝 面试求职: 「面试试题小程序」 ,内容涵盖 测试基础、Linux操作系统、MySQL数据库、Web功能测试、接口测试、APPium移动端测试、Python知识、Selenium自动化测试相关、性能测试、性能测试、计算机网络知识、Jmeter、HR面试,命中率杠杠的。(大家刷起来…)

📝 职场经验干货:

软件测试工程师简历上如何编写个人信息(一周8个面试)

软件测试工程师简历上如何编写专业技能(一周8个面试)

软件测试工程师简历上如何编写项目经验(一周8个面试)

软件测试工程师简历上如何编写个人荣誉(一周8个面试)

软件测试行情分享(这些都不了解就别贸然冲了.)

软件测试面试重点,搞清楚这些轻松拿到年薪30W+

软件测试面试刷题小程序免费使用(永久使用)


我们在使用Selenium测试Web或Electronjs/Cef框架应用时,有时候操作一个元素需要判断是否发送了请求以及请求的参数是否正确

我们可以通过,开启Chrome的性能日志来然后配合driver.get_log(“performance”)来查看请求,然后对Network相关的日子进行过滤, 实现如下: ## 获取Chrome性能日志

  import json
  from pprint import pprint
  from selenium import webdriver
  caps = {
      'browserName': 'chrome',
      'version': '',
      'platform': 'ANY',
      'goog:loggingPrefs': {'performance': 'ALL'},   # 记录性能日志
      'goog:chromeOptions': {'extensions': [], 'args': ['--headless']}  # 无界面模式
  }
  driver = webdriver.Chrome(desired_capabilities=caps)
  driver.get('https://httpbin.org/get')
  logs = driver.get_log("performance")
  for item in logs:
      log = json.loads(item["message"])["message"]
      pprint(log)
      if "Network.response" in log["method"] or "Network.request" in log["method"] or "Network.webSocket" in log["method"]:
          pprint(log)

运行结果如下:

  {'method': 'Network.responseReceived',
   'params': {'frameId': '2445B94E9E1DB51A1B1F4F3B0A3F03F5',
              'loaderId': 'D0DE1754D5C5F1E54DC3B0DB2A09ADD6',
              'requestId': 'D0DE1754D5C5F1E54DC3B0DB2A09ADD6',
              'response': {'connectionId': 0,
                           'connectionReused': False,
                           'encodedDataLength': -1,
                           'fromDiskCache': False,
                           'fromPrefetchCache': False,
                           'fromServiceWorker': False,
                           'headers': {'Content-Type': 'text/plain;charset=US-ASCII'},
                           'mimeType': 'text/plain',
                           'protocol': 'data',
                           'remoteIPAddress': '',
                           'remotePort': 0,
                           'securityState': 'secure',
                           'status': 200,
                           'statusText': 'OK',
                           'url': 'data:,'},
              'timestamp': 57524.763168,
              'type': 'Document'}}
  {'method': 'Network.requestWillBeSent',
   'params': {'documentURL': 'https://httpbin.org/get',
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值