Select a Subtopic
✅ Day 3: Basic Browser Interaction with Selenium
Today's focus is on interacting with the browser elements using Selenium. You'll learn how to:
- 1️⃣ Click buttons
- 2️⃣ Type text into input fields
- 3️⃣ Submit forms
By the end of this session, you'll be able to automate Instagram login using dummy credentials.
🧑💻 Step 1: Setting Up Selenium
Ensure you have everything set up:
pip install selenium
Download Chromedriver and place it in your project folder:Download Chromedriver
📄 Code to Automate Instagram Login
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
import time
# Setup Chrome options
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--start-maximized")
# Initialize the driver
service = Service("chromedriver") # Update path to your chromedriver
driver = webdriver.Chrome(service=service, options=chrome_options)
# Step 1: Open Instagram
driver.get("https://www.instagram.com/")
# Step 2: Wait for the page to load
time.sleep(5)
# Step 3: Find input fields and login button
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']")
# Step 4: Enter credentials
username_field.send_keys("your_dummy_username")
password_field.send_keys("your_dummy_password")
# Step 5: Click the login button
login_button.click()
# Step 6: Wait and close the browser
time.sleep(10)
driver.quit()
🔍 Explanation
driver.get(url): Opens the Instagram website.
find_element(By.NAME, "username"): Finds the username input field by its name attribute.
send_keys(): Sends text input to the input field.
click(): Clicks the Login button to submit the form.
time.sleep(): Waits for a few seconds to allow the page to load.
🛠️ Practical Task
Automate the Instagram login using dummy credentials.
Bonus Task:
Try logging in using different XPath selectors for the login button.
Would you like code snippets for XPath variations? 😊