HTML

    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

    1

    Replace time.sleep() with WebDriverWait

    Update your login script to use explicit waits instead of fixed delays

    2

    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? 😊