Home >Backend Development >Python Tutorial >How to Efficiently Pause Selenium WebDriver Execution in Python?
Question: How can I pause Selenium WebDriver execution for milliseconds in Python?
Answer:
While the time.sleep() function can be used to suspend execution for a specified number of seconds, it's generally not recommended in Selenium WebDriver automation.
Instead, Selenium provides the WebDriverWait class in conjunction with expected conditions to validate an element's state. Here are the common expected conditions:
Example:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() wait = WebDriverWait(driver, 10) # Timeout after 10 seconds # Wait until an element is clickable element = wait.until(EC.element_to_be_clickable((By.ID, "some_button"))) element.click()
This method is preferred over time.sleep() as it avoids unnecessary waiting and checks for the element's desired state before proceeding, improving the efficiency of your tests.
References:
For more information, refer to:
The above is the detailed content of How to Efficiently Pause Selenium WebDriver Execution in Python?. For more information, please follow other related articles on the PHP Chinese website!