python seleinum获取network响应信息

import json

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

capabilities = DesiredCapabilities.CHROME
capabilities["goog:loggingPrefs"] = {
    "performance": "ALL"
}

driver = webdriver.Chrome(
        r"./chromedriver_93",
        desired_capabilities=capabilities,
)

driver.get("https://www.rkengler.com")
logs = driver.get_log("performance")
for log in logs:
"""
log内容
{'method': 'Network.responseReceived', 'params': {'loaderId': '', 'requestId': '94BA56CDA0DE3EBBD05F45534AC7903E', 'response': {'connectionId': 122, 'connectionReused': True, 'encodedDataLength': 478, 'fromDiskCache': False, 'fromPrefetchCache': False, 'fromServiceWorker': False, 'headers': {'access-control-allow-credentials': 'true', 'access-control-allow-headers': 'X-Requested-With,Content-Type,Cache-Control,Pragma,Date,X-Timestamp,Content-Encoding,api-version,Authorization,authorizationv2,Access-Control-Allow-Origin,lang,token,X-Common-Message,X-B3-TraceId,X-B3-SpanId,X-B3-Sampled,version,platform,uuid,x-matchpath,X-B3-SessionId,shuidi-app-code,auth-app-code', 'access-control-allow-methods': 'HEAD, POST, GET, OPTIONS, DELETE, PUT', 'access-control-allow-origin': 'https://xxx.com', 'access-control-expose-headers': 'Content-Disposition,Etag', 'access-control-max-age': '86400', 'date': 'Mon, 27 Sep 2021 06:48:48 GMT', 'server': 'openresty'}, 'mimeType': '', 'protocol': 'h2', 'remoteIPAddress': '140.143.181.70', 'remotePort': 443, 'responseTime': 1632725328677.872, 'securityState': 'secure', 'status': 204, 'statusText': '', 'timing': {'connectEnd': -1, 'connectStart': -1, 'dnsEnd': -1, 'dnsStart': -1, 'proxyEnd': -1, 'proxyStart': -1, 'pushEnd': 0, 'pushStart': 0, 'receiveHeadersEnd': 12.489, 'requestTime': 1623670.725099, 'sendEnd': 0.289, 'sendStart': 0.219, 'sslEnd': -1, 'sslStart': -1, 'workerFetchStart': -1, 'workerReady': -1, 'workerRespondWithSettled': -1, 'workerStart': -1}, 'url': 'https://xxx.com/api/task-info?taskId=1'}, 'timestamp': 1623670.738306, 'type': 'Preflight'}}
"""

    log = json.loads(log["message"])["message"]
    if "Network.responseReceived" in log["method"] and "params" in log.keys():
        body = driver.execute_cdp_cmd('Network.getResponseBody', {
            'requestId': log["params"]["requestId"]
        })
        print(body)
        break

driver.quit()
body打印内容
{'base64Encoded': False, 'body': '...\n'}

你可能感兴趣的:(python全栈工程师,python)