Skip to content
tezvyn:

How would you scrape a page with dynamically loaded JavaScript content?

Source: playwright.devMediumHow cards are made

It tests if you know dynamic pages need a real renderer. A great answer names Playwright or Selenium, uses explicit waits for elements, and extracts via DOM or network interception. Red flag: suggesting only static parsers like BeautifulSoup or blind sleeps.

What's really being asked

This question tests whether you understand the gap between static HTML and a fully rendered modern web application. Interviewers want to see that you know JavaScript frameworks load content after the initial document and that standard HTTP clients cannot execute that code. They also care if you can choose the right tool and write reliable automation rather than brittle scripts.

The full answer

First, name a real browser engine approach such as Playwright, Selenium, or Puppeteer. Second, explain that you wait for the specific content rather than sleeping. A senior answer cites Playwright's auto-waiting behavior where actions wait for elements to become actionable and visible. Third, describe two extraction paths either scraping from the fully rendered DOM once the target element appears or intercepting the network request that feeds the dynamic data which is often faster and more stable. Fourth, mention handling hydration issues where a page shows static HTML before listeners attach; the fix is to wait for the element to be actionable not just present. Fifth, note practical concerns like rate limiting user agent rotation and session management.

The mistakes people make

The biggest red flag is suggesting requests combined with BeautifulSoup or raw regex because these tools do not run JavaScript and will see an empty shell. Another mistake is using fixed time delays such as sleep five seconds instead of explicit waits which makes the scraper slow and flaky under varying network conditions. A third error is ignoring anti-bot protections and pretending a headless browser is invisible without mentioning stealth plugins or proxy rotation.

What usually comes next

The interviewer may ask how you would scale this to thousands of pages per hour which leads to discussion about distributed crawling proxy pools and respecting robots dot txt. They might also ask how to reduce overhead if only a small part of the page is dynamic which opens the door to hybrid approaches using a headless browser just for the JavaScript payload then falling back to static parsing. Another follow up is how you would handle authentication or sessions in Playwright which involves saving storage state and reusing contexts.

A concrete example

Suppose you need to scrape product prices from a single page application built in React. The initial HTML contains no prices. You would launch a Playwright page navigate to the URL and use page dot getByText or a locator to find the price element. Playwright auto waits for that element to appear so you do not need a manual sleep. If the site fires the load event before React hydrates the DOM you might wait for network idle or use page dot waitForURL to ensure navigation is committed. Alternatively you could intercept the underlying API call with page dot route to grab the JSON directly before it ever hits the DOM. This avoids rendering overhead and yields structured data.

Interview question

When scraping a React site where the initial HTML lacks the target data, which tool and technique combination is most reliable?

  • a.Selenium with a fixed ten-second sleep after loading the page before reading the source
  • b.requests paired with BeautifulSoup, retrying the HTTP request until the element is found
  • c.A headless browser that extracts the page source immediately after the navigation event fires
  • d.Playwright using an explicit wait for the target element to appear in the rendered DOMCorrect
Why?

Dynamic JavaScript frameworks inject content after the initial document loads, so only a real browser engine with an explicit wait can reliably capture it. While Selenium is a valid browser tool, using a fixed sleep makes the scraper slow and flaky under variable network conditions rather than waiting for the element to actually appear.

Just read this? Test yourself on what you have been reading.

Read the original → playwright.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles