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
, andCSS 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:
from selenium.webdriver.common.by import By
username_field = driver.find_element(By.NAME, "username")
🖱️ Step 2: How to Inspect Elements in Instagram
- Open Instagram in Chrome: Go to https://www.instagram.com.
- Right-click on the element you want to inspect (e.g., the username input field).
- Click “Inspect” to open Chrome DevTools.
- 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.). 😊