我正在尝试使用 Selenium 从网站上抓取餐厅列表,但我总是收到错误 NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector ":".//*[@id="lista-encontrada"]/div[2]/div[1]/h4"}
我搜索过类似的错误,但它总是与 frame 或 iframe 相关,但我似乎无法在这个特定的网站中找到它们。代码如下:
from selenium import webdriver
url = 'https://www.restaurantemadero.com.br/pt/restaurante/sp/sao-paulo'
driver = webdriver.Chrome()
driver.get(url)
driver.maximize_window()
restaurants = driver.find_elements_by_class_name('blocos')
nome_loja = restaurants[0].find_element_by_xpath('.//*[@id="lista-encontrada"]/div[7]/div[1]/h4')
任何提示都会非常有帮助!
答案 0 :(得分:0)
要获取 restaurant name
,请使用以下 xpath。 //div[@class='blocos']//h4
以下代码将在列表中返回 restaurant name
使用文本的值:
print([res.text for res in driver.find_elements_by_xpath("//div[@class='blocos']//h4")])
或 使用 textContent 的值:
print([res.get_attribute("textContent") for res in driver.find_elements_by_xpath("//div[@class='blocos']//h4")])