I am using Python Selenium. When I try this:
try:
element = driver.find_element_by_xpath('--path--').text.encode('ascii','ignore')
except NoSuchElementException:
pass
print element
But, when element does not exist, shown error
NameError: name 'element' is not defined
..when it should just pass
when can't find the element.
The error is because you are setting the element in the try part only. Either set 'element'
outside try-except or in except
.
Ex:
try:
element = driver.find_element_by_xpath('--path--').text.encode('ascii','ignore')
except NoSuchElementException:
element = None