Introduction
Collecting performance metrics helps you understand how fast and smooth a website works. It shows if pages load quickly or if something slows them down.
performance = driver.execute_script('return window.performance.timing')performance = driver.execute_script('return window.performance.timing') print(performance['loadEventEnd'] - performance['navigationStart'])
performance = driver.execute_script('return window.performance.getEntriesByType("resource")') for resource in performance: print(resource['name'], resource['duration'])
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--headless') service = Service() driver = webdriver.Chrome(service=service, options=options) driver.get('https://example.com') performance = driver.execute_script('return window.performance.timing') load_time = performance['loadEventEnd'] - performance['navigationStart'] print(f'Page load time: {load_time} ms') driver.quit()
nav_start = driver.execute_script('return window.performance.timing.navigationStart;')
load_end = driver.execute_script('return window.performance.timing.loadEventEnd;')
load_time = load_end - nav_start
print(load_time)nav_start = driver.execute_script('return window.performance.timing.navigationStart')
load_end = driver.execute_script('return window.performance.timing.loadEventEnd')
load_time = nav_start - load_end
print(load_time)Pass the test and this pattern moves unknown