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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.