Home >Backend Development >Python Tutorial >How Can I Precisely Sleep in Selenium WebDriver Using Python, and What Are Better Alternatives to `time.sleep()`?
While the time library provides time.sleep(sec) for suspending execution, achieving precision of 250 milliseconds requires specifying a fractional number of seconds:
import time time.sleep(0.25) # Sleep for 250 milliseconds
However, excessive use of time.sleep(sec) is discouraged in Selenium WebDriver automation, as it can hinder efficiency.
Instead, consider using WebDriverWait() in conjunction with expected_conditions(). Three commonly used conditions include:
presence_of_element_located:
visibility_of_element_located:
element_to_be_clickable:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "my_button")))
This example waits for up to 10 seconds for the element with the ID "my_button" to become clickable before continuing.
The above is the detailed content of How Can I Precisely Sleep in Selenium WebDriver Using Python, and What Are Better Alternatives to `time.sleep()`?. For more information, please follow other related articles on the PHP Chinese website!