0

我使用 Pyppeteer 库,因为有一个 Chrome 开发工具协议,并且在 UI 测试中发送请求后我可以接收 webSocketFrameReceived。我能够将套接字响应打印到终端,但这不是我想要的。我需要根据响应参数之一的状态(在屏幕截图https://www.screencast.com/t/4wKSIcPjL9T上标记)以不同的方式继续我的测试。我怎样才能做到这一点?如何处理套接字响应?保存 Python 字典的答案?任何想法

import asyncio
from pyppeteer import launch
import time
import pprint
import json


async def test():
    args = ['--start-maximized']
    browser = await launch(
        args=args,
        headless=False,
        # args=['--no-sandbox'],
        # autoClose=False
        )

    page = await browser.newPage()
    await page.goto('https://example.com/')
    await page.setViewport({'width': 1920, 'height': 1080})

    # create CDP Session
    cdp = await page.target.createCDPSession()
    await cdp.send('Network.enable')

    #  Login
    await page.type('input[name=login]', 'login')
    await page.type('input[name=password]', 'password')
    await page.click('button[type=submit]')
    time.sleep(2)

    # Fill in the request
    await page.type('input[name=symbol]', 'ACY')
    await page.type('input[name=quantity]', '500')
    await page.click('selector')
    await page.type('selector', 'Text')
    await page.keyboard.press('Enter')
    await page.click('button[type=submit]')

    # Waiting for the status of "Pending"
    await page.waitForSelector('selector, {'timeout': 5000})

    def printResponse(response):
        pprint.pprint(response)

    # Calls printResponse when a websocket is received
    cdp.on('Network.webSocketFrameReceived', printResponse) 

    # Calls printResponse when a websocket is sent 
    # cdp.on('Network.webSocketFrameSent', printResponse)  

    await asyncio.sleep(20)
    # await page.close()
    # await browser.close()


asyncio.get_event_loop().run_until_complete(test_best_location_accept())
4

1 回答 1

0
 def printResponse(response):
        pprint(type(response))

将显示响应的类型,这些是 Python 字典

于 2021-01-19T16:16:06.463 回答