我想把每个商家的名字打印在这一页上。我试过这个:
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)但是,当我运行代码时,它打印出一个巨大的空格,没有任何单词。
发布于 2021-05-30 04:15:47
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发布于 2021-05-30 04:04:30
请尝试在那里获取“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']")https://stackoverflow.com/questions/67755327
复制相似问题