【问题标题】:How to access the values of Chrome's Dev tools Network tab's Request or summary using Selenium in python/java?如何在 python/java 中使用 Selenium 访问 Chrome 的开发工具网络选项卡的请求或摘要的值?
【发布时间】:2018-04-06 19:19:08
【问题描述】:

我正在使用 chrome 选项来访问使用 selenium 的性能日志记录,我正在尝试编写一个代码来帮助我确定加载完成后 http 请求的总数和页面的大小。手动我们可以使用开发工具的网络选项卡来检查这一点。只需要知道如何访问网络表的值或汇总值。因为性能日志没有给我我需要的汇总值,所以我想写一个代码来获取:

请求总数=

页面的总权重是多少=

如果可能的话。

Screen shot of network tab highlighted the Summary and Request table values that i need to access

capabilities = DesiredCapabilities.CHROME
capabilities['loggingPrefs'] = {'browser': 'DEBUG'}
capabilities['loggingPrefs'] = {'performance': 'ALL'}
capabilities['perfLoggingPrefs'] = {'enableTimeline': 'true'}


driverLocation = "/Users/harisrizwan/Selenium/chrome/chromedriver"
os.environ["chrome.driver"] = driverLocation
chrome_options = Options()
chrome_options.add_argument("headless")

driver= 
webdriver.Chrome(driverLocation,desired_capabilities=capabilities)
driver.implicitly_wait(10)
driver.maximize_window()
baseUrl="www.google.com"
driver.get(baseUrl)

使用 pandas 创建日志的数据框。

df = pd.DataFrame(driver.get_log('performance'))
df.to_clipboard(index=False)

谢谢。

【问题讨论】:

    标签: java python-2.7 selenium automation google-chrome-devtools


    【解决方案1】:

    我不确定计算页面的总重量,但可以获取发送到服务器的请求总数

    使用https://github.com/lightbody/browsermob-proxy并将代理添加到desired_capabilities,一旦脚本完成,将har文件转储到json并获取所有请求

    在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()
    

    【讨论】:

      【解决方案2】:

      您可以使用performance API 来获取传输的大小。

      主页和每个资源的传输大小:

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

      仅为主页传输大小:

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

      主页和资源的总传输大小:

      size = driver.execute_script("""
        return performance.getEntries()
          .filter(e => e.entryType==='navigation' || e.entryType==='resource')
          .reduce((acc, e) => acc + e.transferSize, 0)
        """)
      

      【讨论】:

        【解决方案3】:

        2021 年更新:

        这现在可以作为 2020/2021 年 Selenium 4 Beta 的一部分。整个 chrome 开发工具套件可通过本机 selenium API 获得。

        见:https://www.selenium.dev/documentation/support_packages/chrome_devtools/

        【讨论】:

          猜你喜欢
          • 2017-12-03
          • 2020-09-01
          • 2019-02-12
          • 2018-09-12
          • 1970-01-01
          • 2021-10-26
          • 2018-04-29
          • 2012-08-26
          • 2018-07-02
          相关资源
          最近更新 更多