Select a Subtopic
π Waiting Strategies in Selenium
Today's focus is on waiting strategies in Selenium. Instagram pages can take time to load, so it's essential to ensure elements are fully loaded before interacting with them. You'll learn:
- β Basic Wait using `time.sleep()`
- β Implicit Wait
- β Explicit Wait with `WebDriverWait`
- β Using Expected Conditions
π§βπ» Basic Wait using `time.sleep()`
This is the simplest way to make your script pause for a few seconds.
π Code Example:
import time
time.sleep(5)
β οΈ Limitation:
`time.sleep()` pauses the execution regardless of whether the element is ready. It's better to use dynamic waits for better performance.
import time
time.sleep(5)
π Explanation:
- driver.implicitly_wait(10): The driver will wait for up to 10 seconds before throwing an exception if an element isnβt found.
π§βπ» **Step 2: Implicit Wait**
An implicit wait tells Selenium to wait for a set amount of time before throwing an exception if an element isnβt found.
π Code Example:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--start-maximized")
service = Service("chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.implicitly_wait(10)
driver.get("https://www.instagram.com/")
π Explanation:
- driver.implicitly_wait(10): The driver will wait for up to 10 seconds before throwing an exception if an element isnβt found.
π§βπ» **Step 3: Explicit Wait with `WebDriverWait`**
An explicit wait waits for a specific condition to be met before proceeding.
π Import Required Classes:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
π Code Example:
# Wait for the login button to appear and then click it
wait = WebDriverWait(driver, 10)
login_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']")))
login_button.click()
π Explanation:
- WebDriverWait(driver, 10): Waits for up to 10 seconds.
- EC.element_to_be_clickable(): Waits until the element is clickable before interacting with it.
π οΈ **Practical Task**
Modify your Instagram login script to use explicit waits instead of `time.sleep()`.
β Full Code with Explicit Wait:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--start-maximized")
service = Service("chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)
driver.get("https://www.instagram.com/")
wait = WebDriverWait(driver, 10)
username_field = wait.until(EC.presence_of_element_located((By.NAME, "username")))
password_field = wait.until(EC.presence_of_element_located((By.NAME, "password")))
login_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']")))
username_field.send_keys("your_dummy_username")
password_field.send_keys("your_dummy_password")
login_button.click()
wait.until(EC.presence_of_element_located((By.XPATH, "//div[text()='Home']")))
print("Login successful!")
driver.quit()
π― Today's Tasks
Replace time.sleep()
with WebDriverWait
Update your login script to use explicit waits instead of fixed delays
Add Home Icon Verification
Implement an explicit wait for Instagram's "Home" icon to confirm successful login
Would you like tips on handling Instagram pop-ups next? π