I use selenium to automate my process within a website. What happens is that I want it to keep loading the page until it finds an element.
The element will appear when I do something from another tab on the page.
I get the following error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/main/div[2]/div[3]/div[1]/div/div[3]/div[2]/div/span"}
But, it doesn't appear right away. I tried the following code and it didn't work:
from selenium.common.exceptions import NoSuchElementException
try:
time.sleep(3)
search = driver.find_element_by_xpath("/html/body/main/div[2]/div[3]/div[1]/div/div[3]/div[2]/div/span")
print('found')
time.sleep(3)
search.send_keys('test')
except NoSuchElementException:
print('not found')
pass
The page reloads, but it only does it once, not infinitely.
Solution
while True:
delay()
try:
driver.find_element_by_xpath("/html/body/main/div[2]/div[3]/div[1]/div/div[3]/div[2]/div/span")
print('Found product in cart')
delay()
boton_finalizar_compra = driver.find_element_by_xpath('/html/body/main/div[2]/div[3]/div[5]/div[2]/div/div[1]/button')
boton_finalizar_compra.click()
except NoSuchElementException:
print('Not found product in cart')
driver.refresh()