Reputation: 1
I am trying to search a webpage for a link text and I'm trying to make the program respond without an error if the item is not found and move onto something else.
from selenium.common.exceptions import NoSuchElementException
hasElementQ = True
while True:
try:
elem = browser.find_element_by_link_text('pant')
break
except NoSuchElementException:
print ('it doesnt exist')
This is the error I receive:
except NoSuchElementException:
^
SyntaxError: invalid syntax
Upvotes: 0
Views: 169
Reputation: 2563
Your except
needs to be at the same indentation level as your try
from selenium.common.exceptions import NoSuchElementException
hasElementQ = True
while True:
try:
elem = browser.find_element_by_link_text('pant')
break
except NoSuchElementException:
print ('it doesnt exist')
Upvotes: 1