需要帮助使用 Selenium-Chromedriver 和 Python3,浏览器自动化

Need help using Selenium-Chromedriver and Python3, browser automation

我想在此页面上打印每个商家的每个名称。我试过这个:

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)

但是,当我 运行 代码时,它会打印一个巨大的空白 space,没有任何字眼。

请尝试在此处获取“值”属性,而不是 span.text

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

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

此外,不要忘记在

之前添加一些等待/延迟
Names = browser.find_elements_by_xpath("//span[@class='merchant_name']")

1 您需要获取 alt 属性才能获取卖家名称

2 您需要等待。

3 打印列表值时检查缩进。

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"))

打印:

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