You want to measure the page load time of a website using Selenium in Python. Which code snippet correctly extracts the loadEventEnd and navigationStart timing values and calculates the load time in milliseconds?
from selenium import webdriver driver = webdriver.Chrome() driver.get('https://example.com') # Your code here to get load time load_time = driver.execute_script('return window.performance.timing.loadEventEnd - window.performance.timing.navigationStart;') print(load_time) driver.quit()
Look for the difference between the event when the page finishes loading and when navigation started.
The loadEventEnd marks when the load event finished, and navigationStart marks when navigation started. Their difference gives the total page load time.
Which of the following window.performance.timing properties represents the moment when the browser starts fetching the document?
Think about when the browser actually begins to request the document from the server.
fetchStart marks the time when the browser starts fetching the document, including any redirects. navigationStart is when navigation begins but may be before fetching.
You want to get the First Contentful Paint (FCP) metric using Selenium's execute_script method. Which code snippet correctly retrieves FCP from the Performance API?
fcp = driver.execute_script(''' const perfEntries = performance.getEntriesByType('paint'); for (const entry of perfEntries) { if (entry.name === 'first-contentful-paint') { return entry.startTime; } } return -1; ''')
Use getEntriesByType('paint') and find the entry with name first-contentful-paint.
The Performance API's paint entries include 'first-contentful-paint'. Using find on getEntriesByType('paint') is the correct way to get its startTime. Option D fails because getEntriesByName is not guaranteed to work for paint entries. Option D is invalid because performance.timing does not have firstContentfulPaint. Option D returns the first paint entry which may be 'first-paint', not 'first-contentful-paint'.
You run this code to get navigation timing but get an error: JavascriptException: window.performance is undefined. What is the most likely cause?
timing = driver.execute_script('return window.performance.timing')Check what page is loaded before accessing performance data.
If the page is about:blank or empty, window.performance may be undefined or have no timing data. The Performance API is standard and available in modern browsers, so B is unlikely. A is incorrect because window.performance exists early. C is the most likely cause when the page is blank.
You want to collect loadEventEnd, domContentLoadedEventEnd, and first-contentful-paint metrics in a single Selenium execute_script call. Which code snippet correctly returns all three as a dictionary?
Calculate relative times from navigationStart and correctly find the FCP entry.
Option A correctly returns relative times for loadEventEnd and domContentLoadedEventEnd by subtracting navigationStart, which is common practice for meaningful metrics. It also uses filter to find the 'first-contentful-paint' entry safely. Option A returns absolute timestamps which are less useful. Option A uses getEntriesByName which may not work for paint entries. Option A returns the first paint entry which may not be FCP.