How to access the values of Chrome's Dev tools Network tab's Request or summary using Selenium in python/java?

前端 未结 2 1606
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 07:40

I\'m using chrome option to access the performance logging using selenium, I\'m trying to write a code that would help me figure out the total number of the http request and

相关标签:
2条回答
  • 2020-12-16 07:58

    You could use the performance API to get the transferred size.

    Transferred size for the main page and each resources:

    sizes = driver.execute_script("""
      return performance.getEntries()
        .filter(e => e.entryType==='navigation' || e.entryType==='resource')
        .map(e=> ([e.name, e.transferSize]));
      """)
    

    Transferred size for the main page only:

    size = driver.execute_script("""
      return performance.getEntriesByType('navigation')[0].transferSize;
      """)
    

    Total transferred size for the main page and resources:

    size = driver.execute_script("""
      return performance.getEntries()
        .filter(e => e.entryType==='navigation' || e.entryType==='resource')
        .reduce((acc, e) => acc + e.transferSize, 0)
      """)
    
    0 讨论(0)
  • 2020-12-16 08:12

    I"m not sure on calculating total weight of the page but obtaining total number of request sent to the server is possible

    Use https://github.com/lightbody/browsermob-proxy and add the proxy to the desired_capabilities, once the script finished, dump the har file to json and get all the request

    in python:

    from browsermobproxy import Server
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    import json
    
    server = Server('path to the proxy server file')
    server.start()
    proxy = server.create_proxy()
    
    options = Options()
    options.add_argument(f'--proxy-server={proxy.proxy}')
    driver = webdriver.Chrome('path to chromedriver', desired_capabilities=options.to_capabilities())
    
    proxy.new_har()
    driver.get('https://www.google.com')
    
    result = json.dumps(proxy.har)
    json_data = json.loads(result)
    
    request= [x for x in json_data['log']['entries']]
    server.stop()
    driver.close()
    
    0 讨论(0)
提交回复
热议问题