This guide shows you how to use Selenium WebDriver to capture full-page screenshots of Indian ecommerce product pages (like those on Flipkart, Amazon.in, or Myntra). You’ll learn to handle dynamic content, lazy-loaded images, and pop-ups that often block the view. By the end, you’ll have a reusable Python script that saves clean, complete screenshots for documentation, testing, or price tracking.
What You’ll Need
- Python 3.7+ installed on your system
- Google Chrome or Firefox browser
- ChromeDriver or GeckoDriver matching your browser version
- Basic familiarity with Python and command line
- Install Selenium:
pip install selenium
Step-by-Step
- Set up the WebDriver. Download the appropriate driver for your browser. For Chrome, place
chromedriverin a known path or add it to your system PATH. Then in Python:from selenium import webdriver from selenium.webdriver.chrome.options import Options opts = Options() opts.add_argument("--start-maximized") opts.add_argument("--disable-notifications") driver = webdriver.Chrome(options=opts) - Navigate to the product page. Use
driver.get(url). For Indian ecommerce sites, you may need to handle a location or language pop-up first. Wait for the page to load using an explicit wait:from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By wait = WebDriverWait(driver, 15) wait.until(EC.presence_of_element_located((By.TAG_NAME, "body"))) - Dismiss pop-ups and banners. Many Indian ecommerce sites show a login modal, a location request, or an app download banner. Look for a close button (often an X icon) and click it if present. Use a try-except to avoid failing if it’s not there:
try: close_btn = driver.find_element(By.XPATH, "//button[contains(@aria-label,'Close')]") close_btn.click() except: pass - Scroll to load all lazy images. Product pages often use lazy loading. Scroll down gradually to trigger image loads. Use JavaScript to scroll in steps:
scroll_pause = 0.5 last_height = driver.execute_script("return document.body.scrollHeight") while True: driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(scroll_pause) new_height = driver.execute_script("return document.body.scrollHeight") if new_height == last_height: break last_height = new_height - Wait for images to fully load. After scrolling, wait a bit more for the images to fetch. You can also check that all images have naturalWidth > 0:
driver.execute_script("window.scrollTo(0, 0);") time.sleep(2) - Take a full-page screenshot. The standard
driver.save_screenshot()only captures the viewport. To get the full page, set the window size to the full page height first:total_height = driver.execute_script("return document.body.scrollHeight") driver.set_window_size(1920, total_height) time.sleep(1) driver.save_screenshot("product_full.png")For extremely long pages, consider using a library like
Selenium-Screenshotor stitching multiple viewport screenshots together. - Save the screenshot. The file will be saved in the current working directory. You can provide a full path to save elsewhere.
- Close the driver. Always clean up:
driver.quit()
Common Mistakes
- Not waiting for dynamic content. Indian ecommerce sites heavily use AJAX. If you take a screenshot immediately, you’ll get a blank or partially loaded page.
- Ignoring pop-ups. A login modal or location prompt will cover the product. Always dismiss them first.
- Wrong window size. If you don’t resize the window, you’ll only capture the visible portion. Set the height to the full page height.
- Forgetting to scroll back to top. Some sites have sticky headers that might overlap content when you scroll back up. Scroll to top and wait before capturing.
- Using implicit waits for everything. Implicit waits can cause timing issues with dynamic elements. Use explicit waits for critical elements.
Tips
- Use headless mode for production. Add
opts.add_argument("--headless")to run without a browser window. In headless mode, you may need to set the window size explicitly. - Handle mobile versions. If you need mobile screenshots, set a mobile user agent and viewport size via Chrome DevTools Protocol.
- Use unique filenames. Include the product ID or timestamp in the filename to avoid overwriting.
- Add a retry mechanism. If the page fails to load, retry after a few seconds.
- Consider using WebDriverWait for specific elements. Wait for the product title or price to be visible before capturing.
Troubleshooting
- Page not loading at all. Check your internet connection. If the site is blocking your IP, try using a different user agent or a VPN. Also, verify the URL is correct.
- Element not found for close button. Inspect the page manually to find the correct selector. The close button might be a
spanordivwith a specific class. Usefind_element(By.CSS_SELECTOR, ".close")or similar. - Screenshot is blank or white. This usually means the page didn’t finish loading. Increase the wait time. Also, ensure you’ve dismissed pop-ups that might cover the content.
- Full-page screenshot is cut off. Some browsers have a maximum window height. For very long pages, use a stitching library like
selenium-screenshotwhich captures multiple viewports and merges them. - Driver crashes or times out. Update your driver to match your browser version. Also, add
opts.add_argument("--disable-gpu")andopts.add_argument("--no-sandbox")if running on Linux.
Frequently Asked Questions
Can I use this for any ecommerce site?
Yes, the general approach works for any website, but you’ll need to adjust selectors for pop-ups and wait conditions based on the site’s structure.
How do I capture only the product image area?
Find the image element and use element.screenshot("image.png") to capture just that element.
Does it work with Firefox?
Yes, replace Chrome options with Firefox options and use GeckoDriver. The rest of the code is similar.
Can I schedule this to run daily?
Yes, you can wrap the script in a cron job or Task Scheduler. Just ensure the driver path is correct and the script handles errors gracefully.
Why is my screenshot blurry?
This can happen if the page is zoomed. Set the device scale factor to 1 using Chrome options: opts.add_argument("--force-device-scale-factor=1").
How do I handle sites that require login?
You can add your login credentials and automate the login process, but be careful with security. Alternatively, use a saved browser profile by specifying opts.add_argument("user-data-dir=/path/to/profile").
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Turn Off Backup Encryption in iTunes?
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It