Given the following link: https://www.ifood.com.br/delivery/brasilia-df/oba-hortifruti---212-sul-asa-sul/8b66f111-5766-409e-97aa-6034868aa230?corredor=4e10f079-d107-437b-b278-e71a964b444b&originArea=aisleMenu, I want to collect information about the prices and descriptions of the products. However, the price displayed in this section is often not the price per kilogram but rather for smaller quantities, such as 500 grams. To get the price per kilogram, I need to click on the specific item I want to collect. For example, clicking on the link https://www.ifood.com.br/delivery/brasilia-df/oba-hortifruti---212-sul-asa-sul/8b66f111-5766-409e-97aa-6034868aa230?corredor=4e10f079-d107-437b-b278-e71a964b444b&originArea=aisleMenu&item=c2fd050a-5f7d-42be-8cfb-e5eea854599d, where the product ID is added to the end of the section link with &item=c2fd050a-5f7d-42be-8cfb-e5eea854599d
, allows me to access the specific product page and obtain the price per kilogram (Preço do quilo) and description. What I am trying to do and am unable to achieve is to have the Selenium code access each product link on the main URL, enter the specific product URL, and collect information about the price per kilogram and description, when this information is available.
What I want is for the code to access the main URL: https://www.ifood.com.br/delivery/brasilia-df/oba-hortifruti---212-sul-asa-sul/8b66f111-5766-409e-97aa-6034868aa230?corredor=4e10f079-d107-437b-b278-e71a964b444b&originArea=aisleMenu. Then, the code should navigate through each of the individual product links found on that URL. Using a loop, the code should collect information about the price per kilogram and the description of each product.
Below is a part of the code that collects information about the description and price on the "main" URL.
list = ["https://www.ifood.com.br/delivery/brasilia-df/oba-hortifruti---212-sul-asa-sul/8b66f111-5766-409e-97aa-6034868aa230?corredor=4e10f079-d107-437b-b278-e71a964b444b&originArea=aisleMenu"]for i in lista: try: driver.get(i) time.sleep(1) scrolls = 5 while scrolls > 0: driver.execute_script("window.scrollTo(0, document.body.scrollHeight)") time.sleep(1) scrolls -= 1 elem = driver.find_element(By.TAG_NAME, "body") elem.send_keys(Keys.END) elem.send_keys(Keys.END) elem.send_keys(Keys.END) time.sleep(1) items = driver.find_elements(By.CLASS_NAME, 'product-card-content') for item in items: descricao = item.find_element(By.CLASS_NAME, 'product-card__description').get_property("innerHTML") try: preco = item.find_element(By.CLASS_NAME, 'product-card__price').get_property("textContent") except: preco = '' item_data = {'descricao': descricao, 'preco': preco} print(json.dumps(item_data, ensure_ascii=False)) except (TimeoutException, WebDriverException) as e: print(f"Erro ao acessar o link {i}: {str(e)}") except Exception as e: print(f"Erro inesperado ao processar o link {i}: {str(e)}")driver.quit()