pythonseleniumselenium-webdriverselenium-chromedriverbrowser-automation

Need help using Selenium-Chromedriver and Python3, browser automation


I would like to print each name of every merchant on this page. I tried this:

browser.get('https://www.trovaprezzi.it/televisori-lcd-plasma/prezzi-scheda-prodotto/lg_oled_cx3?sort=prezzo_totale')

Names = browser.find_elements_by_xpath("//span[@class='merchant_name']")

for span in Names:
    
print(span.text)

However, when I run the code, it prints an huge empty space without any word.


Solution

  • 1 You need to get alt attribute to get a seller name

    2 You need to use waits.

    3 Check your indentation when you print a list values.

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    
    
    browser = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
    
    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")))
    
    
    names = browser.find_elements_by_css_selector(".merchant_name_and_logo img")
    
    for span in names:
        print(span.get_attribute("alt"))
    

    Prints:

    Climaconvenienza
    Shopdigit
    eBay
    ePrice
    Onlinestore
    Shoppyssimo
    Prezzo forte
    eBay
    eBay
    eBay
    eBay
    eBay
    eBay
    Yeppon
    Showprice
    Galagross
    Sfera Ufficio
    Climaconvenienza
    Di Lella Shop
    Shopdigit