0

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.

0

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of span.text please try getting the "value" attribute there

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

for span in Names:
    
    print(span..get_attribute("value"))

Also, don't forget adding some wait / delay before

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

Comments

0

Selenium removed find_elements_by_* in version 4.3.0. See the CHANGES: https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES

* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)

So instead of find_elements_by_xpath(XPATH), use find_elements("xpath", XPATH). And instead of find_elements_by_css_selector(CSS_SELECTOR), use find_elements("css selector", CSS_SELECTOR).

Here's a full script to print all the merchants listed:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.trovaprezzi.it/televisori-lcd-plasma/prezzi-scheda-prodotto/lg_oled_c3?sort=prezzo_totale")
elements = driver.find_elements("css selector", ".merchant_name_and_logo span")
for element in elements:
    print(element.get_attribute("innerText"))
driver.quit()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.