I am trying to scrape the restaurant names of a food delivery website, and the xpaths are randomly numerical. Not all the numbers exist, so when it tries a certain number for x, the error appears. This is some of my input:
restaurantname = ['//li[{}]/a/div[2]/h4'.format(x) for x in range(1, 999)]
for restaurant in restaurantname[:999]:
title = driver.find_element_by_xpath(restaurant).text
print(title)
print('*'*5)
This is the output in my terminal after I run the python file:
Message: no such element: Unable to locate element:
{"method":"xpath","selector":"//li[157]/a/div[2]/h4"}
How do I make it simply skip if there is "no such element?"