D
D
danilr2021-09-16 08:48:34
Python
danilr, 2021-09-16 08:48:34

How to make a loop for Selenium in python with checking the existence of an element on the page?

I have such a problem - selenium goes through the form on the site page and clicks the "Send" button, but after 20-40 seconds an error may occur and the text span "failed to fetch" appears.

How do I press the send button in a while loop, then wait 40 seconds if an error message appears, press the "Send" button again and so on until there is success (success is the message span "Success")?

Friends, please help, I don't understand how to do it with python + selenium implementation. It is necessary to somehow check the existence of this message, but if does not work in the usual sense.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2021-09-16
@moonz

To implement this task, I would recommend using a recursive function.
Selenium has quite a few explicit and implicit wait methods, you can read it in the official documentation
. After the required timeout has elapsed, catch the exception and call the function again.
In order to avoid infinite recursion, it is worth describing the exit scenario from it. For example after 10 attempts.

V
Vitaly Yakovlev, 2021-09-16
@MerzoIT

Auto-testing is usually wrapped in

try:
    ...
except:
    ...

This apphitecture should help you with your problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question