HTML

    Select a Subtopic

    📅 Day 2: Locating Elements on Instagram with Selenium

    Today, you’ll learn how to:

    • Locate web elements using Selenium
    • Use different locator strategies like ID, Class, XPath, and CSS Selectors
    • Practice locating Instagram’s login elements

    🔎 Step 1: Understanding Locator Strategies in Selenium

    To interact with any element on a webpage, you must first locate it using one of the following methods:

    MethodFunctionExample
    By IDfind_element(By.ID, "id_value")driver.find_element(By.ID, "username")
    By Class Namefind_element(By.CLASS_NAME, "class")driver.find_element(By.CLASS_NAME, "button")
    By Namefind_element(By.NAME, "name")driver.find_element(By.NAME, "password")
    By XPathfind_element(By.XPATH, "xpath")driver.find_element(By.XPATH, "//input[@name='username']")
    By CSS Selectorfind_element(By.CSS_SELECTOR, "css")driver.find_element(By.CSS_SELECTOR, "input[name='password']")
    from selenium.webdriver.common.by import By
    
    username_field = driver.find_element(By.NAME, "username")

    🖱️ Step 2: How to Inspect Elements in Instagram

    1. Open Instagram in Chrome: Go to https://www.instagram.com.
    2. Right-click on the element you want to inspect (e.g., the username input field).
    3. Click “Inspect” to open Chrome DevTools.
    4. Look for the unique properties of the element like ID, Class, Name, etc.

    🔧 Step 3: Practice Locating Instagram Login Elements

    1️⃣ Locate the Username Input Field

    from selenium.webdriver.common.by import By
    
    username_field = driver.find_element(By.NAME, "username")

    2️⃣ Locate the Password Input Field

    password_field = driver.find_element(By.NAME, "password")

    3️⃣ Locate the Login Button

    login_button = driver.find_element(By.XPATH, "//button[@type='submit']")

    📄 Full Code: Automating Instagram Login (Without Credentials)

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.chrome.service import Service
    import time
    
    # Path to your ChromeDriver
    service = Service("C:/WebDrivers/chromedriver.exe")
    driver = webdriver.Chrome(service=service)
    
    # Open Instagram
    driver.get("https://www.instagram.com")
    driver.maximize_window()
    
    # Locate elements
    username_field = driver.find_element(By.NAME, "username")
    password_field = driver.find_element(By.NAME, "password")
    login_button = driver.find_element(By.XPATH, "//button[@type='submit']")
    
    # Simulate typing
    username_field.send_keys("your_username")
    password_field.send_keys("your_password")
    
    # Click the login button
    login_button.click()
    
    # Wait and close the browser
    time.sleep(5)
    driver.quit()

    🛠️ Task for Day 2:

    • Locate the username, password, and login button fields on Instagram.
    • Write a script to fill in your credentials and click the login button.
    • Use different locator methods (like XPath, CSS Selector, etc.) to practice.

    💡 Pro Tip:

    If you don’t want to hardcode your Instagram credentials, you can use input() to take them from the user.

    username = input("Enter your Instagram username: ")
    password = input("Enter your Instagram password: ")

    💬 Let me know if you have any issues!Once you’re done, I’ll teach you Day 3: Browser Interactions (Clicking Buttons, Typing Text, etc.). 😊