Handling Dynamic Content with Selenium and Playwright
Modern websites rely heavily on JavaScript. Learn how to scrape dynamic content using browser automation tools like Selenium and Playwright.
The Challenge of Dynamic Content
Modern web applications increasingly rely on JavaScript to load content dynamically. Traditional scraping tools like BeautifulSoup can't handle content that's loaded after the initial page render. This is where browser automation tools become essential.
Understanding Dynamic Content
Dynamic content includes:
- AJAX-loaded data
- Infinite scroll pages
- Single Page Applications (SPAs)
- Content behind user interactions
Selenium: The Industry Standard
Selenium has been the go-to solution for browser automation:
Setting Up Selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Configure Chrome options
options = webdriver.ChromeOptions()
options.add_argument('--headless') # Run in background
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)
Waiting for Dynamic Content
The key to scraping dynamic content is waiting for elements to load:
# Explicit wait
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "dynamic-content")))
# Implicit wait
driver.implicitly_wait(10)
Playwright: The Modern Alternative
Playwright offers better performance and more reliable automation:
Key Advantages
- Faster execution
- Better reliability
- Multiple browser support
- Network interception
- Mobile device emulation
Playwright Example
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://example.com")
# Wait for content to load
page.wait_for_selector(".dynamic-content")
# Extract data
content = page.query_selector_all(".item")
for item in content:
print(item.text_content())
browser.close()
Handling Common Scenarios
Infinite Scroll
# Selenium approach
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
Form Interactions
# Playwright form handling
page.fill("#username", "your_username")
page.fill("#password", "your_password")
page.click("#login-button")
page.wait_for_navigation()
Performance Optimization
Browser automation can be resource-intensive. Here are optimization strategies:
- Disable Images: Save bandwidth and loading time
- Use Headless Mode: Run browsers without GUI
- Block Unnecessary Resources: CSS, fonts, and analytics
- Reuse Browser Sessions: Avoid startup overhead
Error Handling and Reliability
Dynamic scraping requires robust error handling:
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "content"))
)
return element.text
except TimeoutException:
print("Element not found within timeout period")
return None
except NoSuchElementException:
print("Element does not exist")
return None
Best Practices
- Always use explicit waits over implicit waits
- Implement proper exception handling
- Use page object patterns for maintainable code
- Monitor memory usage in long-running scripts
- Implement retry mechanisms for flaky elements
Conclusion
Dynamic content scraping requires more sophisticated tools and techniques, but the payoff is access to the vast majority of modern web content. Both Selenium and Playwright have their strengths – choose based on your specific needs and performance requirements.
Written by
Aashish Timsina
Web scraper and backend developer based in Nepal — building automated data extraction pipelines, SERP scrapers, and scalable Node.js systems.
Need a Custom Web Scraping Solution?
Based in Nepal and working with clients worldwide — let's build a reliable data extraction pipeline tailored to your business.