您现在的位置是:网站首页>文章详情文章详情

Selenium内部日志记录器

inlike2020-04-09 原创文章 浏览(1981) 评论(0) 喜欢(29)

简介Selenium内部日志记录器不能提供浏览器请求的数据,但是可以提供请求相关的性能日志、driver操作日志、浏览器打印的日志记录。

启用driver日志

import json
from selenium import webdriver

caps = {
    'browserName': 'chrome',
    'loggingPrefs': {
        'browser': 'ALL',
        'driver': 'ALL',
        'performance': 'ALL',
    },
    'goog:chromeOptions': {
        'perfLoggingPrefs': {
            'enableNetwork': True,
        },
        'w3c': False,
    },
}
driver = webdriver.Chrome(desired_capabilities=caps)

driver.get('https://www.baidu.com')

log_types = driver.log_types  # 记录的login类型
performance = [json.loads(log['message'])['message'] for log in driver.get_log('performance')]
driver = [json.loads(log['message'])['message'] for log in driver.get_log('driver')]
browser = [json.loads(log['message'])['message'] for log in driver.get_log('browser')]

日志消息-performance

{{'method': 'Network.responseReceived',
  'params': {'frameId': '0E0B69DA47FD5DC0A7E02CD8F76E45E7',
   'loaderId': '0CF2DBB40B8E262FB203D352C244D1E4',
   'requestId': '12292.43',
   'response': {'connectionId': 221,
    'connectionReused': False,
    'encodedDataLength': 284,
    'fromDiskCache': False,
    'fromPrefetchCache': False,
    'fromServiceWorker': False,
    'headers': {'accept-ranges': 'bytes',
     'access-control-allow-origin': '*',
     'age': '54',
     'content-length': '43',
     'content-type': 'image/gif',
     'date': 'Thu, 09 Apr 2020 10:26:34 GMT',
     'etag': '"1714626189"',
     'last-modified': 'Mon, 23 Mar 2020 11:20:15 GMT',
     'ohc-cache-hit': 'cd6ct74 [4]',
     'ohc-response-time': '1 0 0 0 0 0',
     'server': 'JSP3/2.0.14',
     'status': '200',
     'timing-allow-origin': 'http://www.baidu.com'},
    'mimeType': 'image/gif',
    'protocol': 'h2',
    'remoteIPAddress': '118.112.225.33',
    'remotePort': 443,
    'securityDetails': {'certificateId': 0,
     'certificateTransparencyCompliance': 'compliant',
     'cipher': 'AES_256_GCM',
     'issuer': 'GlobalSign Organization Validation CA - SHA256 - G2',
     'keyExchange': '',
     'keyExchangeGroup': 'X25519',
     'protocol': 'TLS 1.3',
     'sanList': ['baidu.com',
      'click.hm.baidu.com',
      'cm.pos.baidu.com',
      'log.hm.baidu.com',
      'update.pan.baidu.com',
      'wn.pos.baidu.com',
      '*.91.com',
      '*.aipage.cn',
      '*.aipage.com',
      '*.apollo.auto',
      '*.baidu.com',
      '*.baidubce.com',
      '*.baiducontent.com',
      '*.baidupcs.com',
      '*.baidustatic.com',
      '*.baifae.com',
      '*.baifubao.com',
      '*.bce.baidu.com',
      '*.bcehost.com',
      '*.bdimg.com',
      '*.bdstatic.com',
      '*.bdtjrcv.com',
      '*.bj.baidubce.com',
      '*.chuanke.com',
      '*.dlnel.com',
      '*.dlnel.org',
      '*.dueros.baidu.com',
      '*.eyun.baidu.com',
      '*.fanyi.baidu.com',
      '*.gz.baidubce.com',
      '*.hao123.baidu.com',
      '*.hao123.com',
      '*.hao222.com',
      '*.im.baidu.com',
      '*.map.baidu.com',
      '*.mbd.baidu.com',
      '*.mipcdn.com',
      '*.news.baidu.com',
      '*.nuomi.com',
      '*.safe.baidu.com',
      '*.smartapps.cn',
      '*.ssl2.duapps.com',
      '*.su.baidu.com',
      '*.trustgo.com',
      '*.xueshu.baidu.com',
      'apollo.auto',
      'baifae.com',
      'baifubao.com',
      'dwz.cn',
      'mct.y.nuomi.com',
      'www.baidu.cn',
      'www.baidu.com.cn'],
     'signedCertificateTimestampList': [{'hashAlgorithm': 'SHA-256',
       'logDescription': "Google 'Skydiver' log",
       'logId': 'BBD9DFBC1F8A71B593942397AA927B473857950AAB52E81A909664368E1ED185',
       'origin': 'Embedded in certificate',
       'signatureAlgorithm': 'ECDSA',
       'signatureData': '304502202C7B4DC0F985478A2D0AC0793BD6B4B566F8AAFB8258AD2336FE16BCA6839921022100C02FCD9C9920CB7D915FD28BC6131073B5C1540333419FA66AC51493CF692B6B',
       'status': 'Verified',
       'timestamp': 1557364924826.0},
      {'hashAlgorithm': 'SHA-256',
       'logDescription': "Sectigo 'Mammoth' CT log",
       'logId': '6F5376AC31F03119D89900A45115FF77151C11D902C10029068DB2089A37D913',
       'origin': 'Embedded in certificate',
       'signatureAlgorithm': 'ECDSA',
       'signatureData': '304502200332689E39D0EB5F1961DBA712696F28448102A53CC2A313D57E98265F201AA0022100A78B62B3B0B44432E211FF458D55112C36AB299344C8345CCE7C355731AEAB12',
       'status': 'Verified',
       'timestamp': 1557364923983.0}],
     'subjectName': 'baidu.com',
     'validFrom': 1557364922,
     'validTo': 1593063062},
    'securityState': 'secure',
    'status': 200,
    'statusText': '',
    'timing': {'connectEnd': 52.386,
     'connectStart': 30.547,
     'dnsEnd': 30.547,
     'dnsStart': 0.595,
     'proxyEnd': 0.576,
     'proxyStart': 0.145,
     'pushEnd': 0,
     'pushStart': 0,
     'receiveHeadersEnd': 58.854,
     'requestTime': 142844.863989,
     'sendEnd': 52.639,
     'sendStart': 52.512,
     'sslEnd': 52.382,
     'sslStart': 35.87,
     'workerReady': -1,
     'workerStart': -1},
    'url': 'https://ss3.baidu.com/6ONWsjip0QIZ8tyhnq/ps_default.gif?_t=1586427994653'},
   'timestamp': 142844.923409,
   'type': 'Image'}},
 {'method': 'Network.dataReceived',
  'params': {'dataLength': 43,
   'encodedDataLength': 0,
   'requestId': '12292.43',
   'timestamp': 142844.923482}},
 {'method': 'Network.loadingFinished',
  'params': {'encodedDataLength': 284,
   'requestId': '12292.43',
   'shouldReportCorbBlocking': False,
   'timestamp': 142844.923187}}]

日志消息-driver

[{'level': 'INFO',
  'message': '[fd2874a1eeb2830d569f5e2fb4a65413] RESPONSE GetLog [ {\n   "level": "INFO",\n   "message": "Populating Preferences file: {\\n   \\"alternate_error_pages\\": {\\n      \\"enabled\\": false\\n   },\\n   \\"autofill\\": {\\n      \\"enabled\\": false\\n   },\\n   \\"browser\\": {\\n      \\"check_default_browser\\": false\\n   },\\n  ...",\n   "timestamp": 1.586427936575e+12\n}, {\n   "level": "INFO",\n   "message": "Populating Local State file: {\\n   \\"background_mode\\": {\\n      \\"enabled\\": false\\n   },\\n   \\"ssl\\": {\\n      \\"rev_checking\\": {\\n         \\"enabled\\": false\\n      }\\n   }\\n}\\n",\n   "timestamp": 1.586427936575e+12\n}, {\n   "level": "INFO",\n   "message": "Cannot switch to US keyboard layout - some keys may be interpreted incorrectly\\n",\n   "timestamp": 1.586427936578e+12\n}, {\n   "level": "INFO",\n   "message": "Launching chrome: \\"C:\\\\Program Files (x86)\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe\\" --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monito...",\n   "timestamp": 1.586427936578e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools HTTP Request: http://localhost:57871/json/version\\n",\n   "timestamp": 1.586427937306e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools HTTP Response: {\\r\\n   \\"Browser\\": \\"Chrome/80.0.3987.163\\",\\r\\n   \\"Protocol-Version\\": \\"1.3\\",\\r\\n   \\"User-Agent\\": \\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) C...",\n   "timestamp": 1.586427937609e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools HTTP Request: http://localhost:57871/json/list\\n",\n   "timestamp": 1.586427937609e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools HTTP Response: [ {\\r\\n   \\"description\\": \\"\\",\\r\\n   \\"devtoolsFrontendUrl\\": \\"/devtools/inspector.html?ws=localhost:57871/devtools/page/0E839BE3932DA0380EEADA647669CD50\\",\\r\\n   \\"id\\": \\"0E839BE3932DA0...",\n   "timestamp": 1.58642793761e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools HTTP Request: http://localhost:57871/json/list\\n",\n   "timestamp": 1.58642793761e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools HTTP Response: [ {\\r\\n   \\"description\\": \\"\\",\\r\\n   \\"devtoolsFrontendUrl\\": \\"/devtools/inspector.html?ws=localhost:57871/devtools/page/0E839BE3932DA0380EEADA647669CD50\\",\\r\\n   \\"id\\": \\"0E839BE3932DA0...",\n   "timestamp": 1.586427937611e+12\n}, {\n   "level": "WARNING",\n   "message": "Timed out connecting to Chrome, retrying...\\n",\n   "timestamp": 1.586427939613e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools WebSocket Command: Network.enable (id=1) 0E0B69DA47FD5DC0A7E02CD8F76E45E7 {\\n\\n}\\n",\n   "timestamp": 1.586427941617e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools WebSocket Command: Log.enable (id=2) 0E0B69DA47FD5DC0A7E02CD8F76E45E7 {\\n\\n}\\n",\n   "timestamp": 1.586427941617e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools WebSocket Command: DOM.getDocument (id=3) 0E0B69DA47FD5DC0A7E02CD8F76E45E7 {\\n\\n}\\n",\n   "timestamp": 1.586427941618e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools WebSocket Command: Target.setAutoAttach (id=4) 0E0B69DA47FD5DC0A7E02CD8F76E45E7 {\\n   \\"autoAttach\\": true,\\n   \\"flatten\\": true,\\n   \\"waitForDebuggerOnStart\\": false\\n}\\n",\n   "timestamp": 1.586427941618e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools WebSocket Command: Page.enable (id=5) 0E0B69DA47FD5DC0A7E02CD8F76E45E7 {\\n\\n}\\n",\n   "timestamp": 1.586427941618e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools WebSocket Command: Page.enable (id=6) 0E0B69DA47FD5DC0A7E02CD8F76E45E7 {\\n\\n}\\n",\n   "timestamp": 1.586427941618e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools WebSocket Response: Network.enable (id=1) 0E0B69DA47FD5DC0A7E02CD8F76E45E7 {\\n\\n}\\n",\n   "timestamp": 1.586427941619e+12\n}, {\n   "level": "DEBUG",\n   "message": "DevTools WebSocket Response: Log.enable (id=2) 0E0B69DA47FD5DC0A7E02CD8F76E45E7 {\\n\\n}\\n",\n   "timestamp": 1.586427941619e+12\n}, "..." ]\n',
  'timestamp': 1586428215728},
 {'level': 'INFO',
  'message': '[fd2874a1eeb2830d569f5e2fb4a65413] COMMAND GetLog {\n   "sessionId": "fd2874a1eeb2830d569f5e2fb4a65413",\n   "type": "driver"\n}\n',
  'timestamp': 1586428225513},
 {'level': 'DEBUG',
  'message': 'DevTools WebSocket Command: Runtime.evaluate (id=57) 0E0B69DA47FD5DC0A7E02CD8F76E45E7 {\n   "awaitPromise": true,\n   "expression": "1",\n   "returnByValue": true\n}\n',
  'timestamp': 1586428225514},
 {'level': 'DEBUG',
  'message': 'DevTools WebSocket Response: Runtime.evaluate (id=57) 0E0B69DA47FD5DC0A7E02CD8F76E45E7 {\n   "result": {\n      "description": "1",\n      "type": "number",\n      "value": 1\n   }\n}\n',
  'timestamp': 1586428225514}]


其它相关文档:https://stackoverflow.com/questions/6509628/how-to-get-http-response-code-using-selenium-webdriver

https://www.jianshu.com/p/615e3c0140a5


很赞哦! ( 29)
    《Python实战进阶》
    None
    None
    夏至已深

站点信息

  • 建站时间:2019-5-24
  • 网站程序:like in love
  • 主题模板《今夕何夕》
  • 文章统计:104条
  • 文章评论:***条
  • 微信公众号:扫描二维码,关注我们
  • 个人微信公众号