Hey everyone, I’m stuck with a web scraping project. I’m trying to get data from an online store using Selenium and Python. My code runs without any errors, but I’m not getting any output. It’s really frustrating!
I’m using Selenium 4.8.2 and the Chrome webdriver opens up fine. I’ve tried different solutions from Stack Overflow, but nothing seems to work. Here’s what I’m doing:
- Opening a product page on the site
- Waiting for the page to load
- Trying to find product info (title, price, rating) using XPath
- Aiming to save the data as JSON
The weird thing is, even when I add print statements, I get nothing. Has anyone run into this before? Any ideas on what I might be missing or doing wrong? I’d really appreciate some help figuring this out!
from selenium import webdriver
from selenium.webdriver.common.by import By
import json
import time
browser = webdriver.Chrome()
browser.minimize_window()
store_url = 'https://example-store.com/product/12345'
browser.get(store_url)
time.sleep(10)
item_containers = browser.find_elements(By.XPATH, '//div[@class="product-card"]')
items_data = []
for container in item_containers:
try:
name = container.find_element(By.XPATH, './/h3[@class="item-name"]').text
except:
name = 'N/A'
try:
cost = container.find_element(By.XPATH, './/span[@class="item-price"]').text
except:
cost = 'N/A'
try:
stars = container.find_element(By.XPATH, './/div[@class="item-rating"]').text
except:
stars = 'N/A'
items_data.append({
'name': name,
'cost': cost,
'stars': stars
})
with open('scraped_data.json', 'w') as f:
json.dump(items_data, f)
browser.quit()
Any help would be awesome!