michael kalantzis
michael kalantzis

Reputation: 1

how to raise error and move on to different function when item not found in selenium

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

Answers (1)

Lucas Tierney
Lucas Tierney

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

Related Questions