我正在尝试在Python中使用selenium.webdriver.chrome从groceries.asda.com提取产品信息
但是,如果我转到特定页面(https://groceries.asda.com/shelf/everyday-family-cereals/2-for-4-cereals/_/3097456947)并在Google Chrome中查看其源代码,则找不到产品名称,因此无法使用诸如find_element_by_xpath之类的方法
有人知道如何使用硒从ASDA网站提取产品名称吗?
尝试过xpath,css选择器,它们都返回NoSuchElementException
from selenium import webdriver
url = "https://groceries.asda.com/shelf/everyday-family-cereals/2-for-4-cereals/_/3097456947"
driver = webdriver.Chrome()
driver.get(url)
nav = driver.find_element_by_xpath('//*[@id="listingsContainer-0bd93fc4-888f-52c5-1c47-5679e4aab507"]/div/div[1]/div[2]/div/div/div[2]/span[1]/a/span')
product = nav.text
print(product)
NoSuchElementException
答案 0 :(得分:0)
尝试这种方法-这应该使用其“ alt”属性打印所有产品详细信息
Products = driver.find_elements_by_xpath("//div[@class=' co-product-list']//a/img")
print(len(Products))
for product in Products:
print(product.get_attribute('alt'))
答案 1 :(得分:0)
感谢您的启发。这按预期工作。谢谢你
Products = driver.find_elements_by_xpath("//div[@class='product-content']//span//a")
for product in Products:
print(product.get_attribute('title'))