Home > Software design >  Using time.sleep with selenium (sleep until something is completly loaded)
Using time.sleep with selenium (sleep until something is completly loaded)

Time:05-25

i'm trying to find a way, to sleep for as long as it takes for an selenium element to be loaded. i tried it with actionchains and with the inbuilt sleep where you can define a max sleep timeout. However after doing so, it still wasn't what i tried to do (card number got cut off on the first part thus the enterd card number wasn't valid. i then added a 10 seconds time.sleep interval which solved the issue however, sometimes it dosn't work just because it takes more time to load (maybe connection is bad) or the sleep interval is set too high what makes the code unncececary slow.

I expected it to work. I create this code for someone else and this person had the issues with it being too fast while on my side everything went flawlessly.

I don't get any error message it's just a question, how i can implement the sleep within time.sleep for as long as it takes for the element to be fully loaded.

I've tried to use Webdriverwait but this didn't did the thing:

WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))

CodePudding user response:

There is no such Expected Condition to wait until the element to be fully loaded.

I don't know exactly what are you trying to get but most probably the element is loaded in DOM so the expected condition is met even if the content of that element is not there yet. If you are trying to access a button you can use the EC.element_to_be_clickable.

If it is something visible on your screen you can use EC.visibility_of_element_located.

The solution here lies in the ExpectedConditions and not in the sleep time which is really random if you will get the content and it will be really slow.

Hope this helps!

  • Related