0

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?

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Yes maybe my example is not good, what should i do for the locators?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.