How to Capture Indian E-Commerce Product Pages with Selenium
In this article
Capturing product pages from Indian e-commerce sites like Flipkart, Amazon.in, or Myntra is a common need for price tracking, market research, or building datasets. This guide walks you through a reliable Selenium + Chrome setup that handles the quirks of these sites—dynamic content, login walls, and bot detection—so you can save clean HTML snapshots or extract structured data. You'll need basic Python knowledge and a willingness to tweak a few settings for stability.
What You'll Need
- Python 3.7+ installed on your system
- Google Chrome browser (latest version recommended)
- ChromeDriver matching your Chrome version (or use Selenium Manager, which auto-downloads it in Selenium 4.6+)
- Required Python packages:
selenium,webdriver-manager(optional but handy),beautifulsoup4(for parsing), andrequests(for saving images if needed) - A stable internet connection and, ideally, a VPN or proxy if you're outside India (some sites geo-restrict)
Step-by-Step
- Set up your environment. Create a new Python file (e.g.,
capture.py) and install the required packages:pip install selenium webdriver-manager beautifulsoup4. If you're using Selenium 4.6 or newer, you can skipwebdriver-managerand let Selenium handle the driver automatically. - Configure Chrome options for stability. Start with a headless browser to avoid UI pop-ups, but disable the automated-infobar and set a realistic user-agent. Use these options:
options.add_argument('--headless=new')options.add_argument('--disable-gpu')options.add_argument('--no-sandbox')options.add_argument('--window-size=1920,1080')options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36') - Initialize the driver. Use
webdriver.Chrome(options=options)orwebdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)if you prefer webdriver-manager. Test it by navigating to a simple page likehttps://example.com. - Handle login walls and pop-ups. Indian e-commerce sites often show a login modal or a location prompt. Before capturing, try to dismiss these. For Flipkart, you might need to click the close button on the login popup. Use
WebDriverWaitto wait for elements to appear, then click them. If you have credentials, you can log in, but for public product pages, you usually don't need to. - Navigate to the product URL. Use
driver.get(url)and then wait for the page to fully load. Since these sites are heavy with JavaScript, usetime.sleep(3)or better, wait for a specific element like the product title to be present withWebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'h1')). - Scroll to trigger lazy loading. Many product pages load images and reviews as you scroll. Execute JavaScript to scroll gradually:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")and wait a couple of seconds. Repeat a few times to load all content. - Capture the page. You have two options: save the full HTML or take a screenshot. For HTML, use
page_source = driver.page_sourceand write it to a file with UTF-8 encoding. For a screenshot, usedriver.save_screenshot('product.png'). For a full-page screenshot, you may need to set the window size to the full page height first. - Extract structured data (optional). Use BeautifulSoup to parse the HTML and extract product name, price, ratings, etc. For example:
soup = BeautifulSoup(page_source, 'html.parser')then find elements by class or CSS selectors. Be aware that selectors change frequently, so inspect the page first. - Close the driver. Always call
driver.quit()in afinallyblock to free resources and avoid leaving zombie processes.
Common Mistakes
- Using a default user-agent. Selenium's default user-agent contains "HeadlessChrome", which is easily blocked. Always set a realistic one.
- Not waiting for elements. Using
time.sleep()is unreliable; prefer explicit waits. If you don't wait, you'll capture a blank page. - Ignoring pop-ups. Flipkart's login modal will block the content. You must close it or the captured HTML will be just the modal.
- Not scrolling. If you don't scroll, lazy-loaded images and reviews won't be in the HTML.
- Running headless without disabling GPU. This causes rendering issues on some systems; always include
--disable-gpu. - Hardcoding selectors. Indian e-commerce sites change their class names often. If you're extracting data, use more stable attributes like
data-testidif available, or update selectors frequently.
Tips
- Use a proxy or VPN. If you're outside India, some sites redirect to international versions or block access. A residential Indian IP helps.
- Slow down. Add random delays between requests to avoid hitting rate limits. For bulk captures, use
time.sleep(random.uniform(2,5)). - Handle CAPTCHAs. If you encounter a CAPTCHA, you can't bypass it easily. Consider using a service like 2Captcha, or manually solve it in non-headless mode.
- Save cookies. If you log in, save cookies to reuse later and avoid repeated logins. Use
pickleto storedriver.get_cookies(). - Use headless with a virtual display. If you need to interact with complex elements, run headless with
--headless=newwhich is more stable in recent Chrome versions. - Monitor memory. Long-running captures can leak memory. Restart the driver periodically if you're scraping many pages.
Troubleshooting
- Page loads but shows "Something went wrong" or a blank screen. This usually means the site detected automation. Try adding the
--disable-blink-features=AutomationControlledoption and settingdriver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")before navigating. - ChromeDriver version mismatch. If you get a session not created error, update ChromeDriver. Using Selenium Manager (v4.6+) or webdriver-manager solves this automatically.
- Login popup won't close. Inspect the popup's HTML to find the correct close button selector. Sometimes it's an iframe; switch to it first with
driver.switch_to.frame(). - Capturing the page takes too long. Reduce the number of scrolls or set a fixed wait time. Also, disable images and CSS to speed up loading:
prefs = {"profile.managed_default_content_settings.images": 2}and add to options. - Getting blocked after a few requests. Implement exponential backoff and rotate user-agents. Consider using a headless browser with a real user profile (launch Chrome with a user-data-dir).
- Full-page screenshot is cut off. Set the window size to the full page height before capturing:
driver.set_window_size(1920, driver.execute_script("return document.body.scrollHeight")).
Frequently Asked Questions
- Is it legal to scrape Indian e-commerce sites? Scraping public data for personal use is generally tolerated, but always check the site's robots.txt and terms of service. Avoid scraping personal data or bypassing login walls.
- Can I use this for Amazon.in? Yes, but Amazon has stricter bot detection. You'll likely need to use a residential proxy and possibly a captcha-solving service.
- How do I get the product price reliably? Prices are often in a with a specific class. Inspect the element and use a CSS selector like
.p-priceorspan.a-price-whole. Be aware that prices may load via AJAX after the initial page load. - Why do I get a different page when I use Selenium vs. my browser? The site may serve different content based on the user-agent or presence of cookies. Ensure you're using the same user-agent and have accepted cookies if needed.
- How can I capture multiple products? Loop through a list of URLs, and for each, navigate, wait, capture, and save. Add a random delay between each to avoid being blocked.
- What if the product page is a single-page app (SPA)? Some sites load content dynamically. You may need to wait for specific elements to appear, or use
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")to trigger loading.