SoFunction
Updated on 2025-03-04

Three Waiting Methods and Pros and Disadvantages of Python

1. Call method

1. Forced waiting

Call time module, use (n), force wait n seconds

2. Implicit waiting

implicitly_wait(n), sets the maximum implicit waiting time n seconds, waits for the element to load, and if the element is not loaded at the time, a NoSuchElementException error will be thrown.

3. Explicitly wait

Calling the WebDriverWait() class, explicit waiting is a way to wait for a specified element. Wait for elements of the page by setting the maximum waiting time and checking the frequency. Once the element is found, stop waiting and enter the next step. Otherwise, continue waiting until the maximum time set is exceeded, and then throw a TimeoutException exception.

from selenium import webdriver
from  import WebDriverWait
from  import expected_conditions as EC
from  import By

driver = ()
('')
# Set browser: driver Waiting time:?wait = WebDriverWait(driver, Number of seconds)# important!# Set judgment conditions: Wait for the element with id='kw' to loadinput_box = (EC.presence_of_element_located((, 'kw')))
# Enter keywords: keywordsinput_box.send_keys('Keyword')
()

2. Pros and cons

1. Forced waiting

Disadvantages: It is relatively rigid and cannot accurately grasp the waiting time. If it is used in large quantities, it will affect the execution efficiency and waste unnecessary time.

Advantages: Simple call, can be used during debugging

2. Implicit waiting

Disadvantages: It is not very flexible to use. The program will wait for the entire page to load before performing the next operation. It requires the waiting time to complete; the page does not load during the waiting time, and the next operation will also enter the next operation as soon as the time reaches. If the element to be located does not appear, an error that the element cannot be found will be reported.

Advantages: It works on the entire driver cycle, and each operation will be implicitly waited, only set once

3. Show waiting

Disadvantages: The use is relatively complex, similar to forced wait, each line is only executed once

Advantages: Waiting for accurate judgment will not waste unnecessary waiting time, which can improve execution efficiency

Summarize

This is the article about the three waiting methods and advantages and disadvantages of python. For more information about the three waiting methods of python, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!