Reputation: 2415
In my python-Selenium code, i should use many try-catch to pass the errors, and if i don't use it, my script doesn't continue.
for example if i don't use try catch, if my script doesn't find desc span
i will have an error and i can't continue,
try :
libelle1produit = prod.find_element_by_css_selector(('.desc span')).text # libelle1produit OK
except:
pass
is there an alternative method for passing error?
Upvotes: 0
Views: 550
Reputation: 23805
is there an alternative method for passing error?
Yes, To avoid try-catch
and avoid error as well try using find_elements
instead which would return either a list of all elements with matching locator or empty list if nothing is found, you need to just check the list length to determine that element exists or not as below :-
libelle1produit = prod.find_elements_by_css_selector('.desc span')
if len(libelle1produit) > 0 :
libelle1produit[0].text
Upvotes: 2