Home > Blockchain >  Python with selenium : Looping trough the first web element in a list
Python with selenium : Looping trough the first web element in a list

Time:04-12

i`m new to python and selenium webdriver, and i have started to give miself some "tasks" to practice. I have the next problem : The following code is identifing the right number of products in page, in pdb if i print the all_products will print: max 12 different web elements;(12 default results on page or less depending on the items available) I want to print each product from this page in a dictionary with "product name" and "product price" But something is wrong, it looks like it loops trough the first web element, the output is :

Number of products: 11 [{'Name': 'Nova 9 SE 128GB Crystal Blue', 'Price': '1599,68'}, {'Name': 'Nova 9 SE 128GB Crystal Blue', 'Price': '1599,68'}, {'Name': 'Nova 9 SE 128GB Crystal Blue', 'Price': '1599,68'}, {'Name': 'Nova 9 SE 128GB Crystal Blue', 'Price': '1599,68'}, {'Name': 'Nova 9 SE 128GB Crystal Blue', 'Price': '1599,68'}, {'Name': 'Nova 9 SE 128GB Crystal Blue', 'Price': '1599,68'}, {'Name': 'Nova 9 SE 128GB Crystal Blue', 'Price': '1599,68'}, {'Name': 'Nova 9 SE 128GB Crystal Blue', 'Price': '1599,68'}, {'Name': 'Nova 9 SE 128GB Crystal Blue', 'Price': '1599,68'}, {'Name': 'Nova 9 SE 128GB Crystal Blue', 'Price': '1599,68'}, {'Name': 'Nova 9 SE 128GB Crystal Blue', 'Price': '1599,68'}]

url = "https://www.orange.ro/magazin-online/telefoane?order=ASC&sort=TopSales&from=0&size=12&filter=Huawei,Apple,Faraabonament1,Noi,Instoc"
driver.get(url)
all_products_class = 'ws-product'
all_products = driver.find_elements(By.CLASS_NAME, all_products_class)
print(f"Number of products: {len(all_products)}")
all_product_price = []

for product in all_products:

  device_name_xpath = ".//span[@class='ws-product-model-name']"
  device_name = driver.find_element(By.XPATH, device_name_xpath)
  name = device_name.text

  device_price_xpath = ".//span[@class='ws-product-price']"
  device_price = driver.find_element(By.XPATH, device_price_xpath)
  price = device_price.text
  
  all_product_price.append({'Name': name, 'Price' : price})

print(all_product_price)
driver.quit

CodePudding user response:

Something seems to be missing in your code.

When looping thru the list of all products and retrieving the details of each product here

device_name_xpath = ".//span[@class='ws-product-model-name']"
device_name = driver.find_element(By.XPATH, device_name_xpath)

you (most likely) must specify a unique property of the product (maybe there is a product id) from your list of all products.

In other words: in your loop you always select the same span having a class with name ws-product-model-name.

CodePudding user response:

I have found the issue in the code :

device_name = driver.find_element(By.XPATH, device_name_xpath)

replaced with :

device_name = product.find_element(By.XPATH, device_name_xpath)

&

device_price = driver.find_element(By.XPATH, device_price_xpath)

replaced with

device_price = product.find_element(By.XPATH, device_price_xpath)
  • Related