Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to run other code when an error occurs?

I need to automatically log into the site, but sometimes a captcha pops up (not always). I have the code ready to run the captcha solving API. How can I activate it only when captcha appears? After opening the site, I have the code:

join = driver.find_element(By.XPATH, "xpath")
join.click()

And if a captcha appears instead of the "join" button, then the error "No such element: Unable to locate element" pops up.
How can I run the captcha solution code after this error and continue the normal code after solving the captcha?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a try except block:

try:
    join = driver.find_element(By.XPATH, "xpath")
    join.click()
except:
    # Your captcha code.

If you know your specific exception you can replace except: with except ExceptionName:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading