Need help using Selenium Chromedriver and Python

Issue

I would like to print each merchant name next to "his" price of the page like this:

Climaconvenienza 1.031,79 €

Hwonline 1.031,80 €

Shopdigit 1.073,90 €

The code I made is this:

browser.get('https://www.trovaprezzi.it/televisori-lcd-plasma/prezzi-scheda-prodotto/lg_oled_cx3?sort=prezzo_totale')
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".merchant_name_and_logo img")))
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".merchant_name_and_logo img"))) 

names = browser.find_elements_by_css_selector(".merchant_name_and_logo img")

for span in names:
    print(span.get_attribute("alt"))
    
all_divs = browser.find_elements_by_xpath("//div[@class='item_total_price']")
for div in all_divs:
    print(div.text)

But, by running my code, I get this:

Climaconvenienza

Hwonline

Shopdigit

1.031,79 €

1.031,80 €

1.073,90 €

Solution

Assuming names and all_divs always have the same length (as they do in your example), the following should work:

browser.get('https://www.trovaprezzi.it/televisori-lcd-plasma/prezzi-scheda-prodotto/lg_oled_cx3?sort=prezzo_totale')
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".merchant_name_and_logo img")))
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".merchant_name_and_logo img"))) 

names = browser.find_elements_by_css_selector(".merchant_name_and_logo img")
all_divs = browser.find_elements_by_xpath("//div[@class='item_total_price']")

for i in range(len(names)):
    print(names[i].get_attribute("alt") + ' ' + all_divs[i].text)
    

Answered By – C. Peck

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published