Select a Subtopic
π Day 5: Navigating Instagram and Handling Pop-Ups with Selenium
Today, we'll take your Instagram automation to the next level by learning:
- β How to navigate Instagram after login
- β Handling pop-ups (like "Save Info" and "Turn on Notifications")
- β Clicking profile elements and navigating to different sections
Let's dive right in!
π§βπ» Step 1: Handling Post-Login Pop-Ups
After logging in, Instagram shows pop-ups like:
- "Save Your Login Info?"
- "Turn on Notifications"
We need to dismiss these pop-ups to continue navigating the website.
π Code to Handle Pop-Ups:
# Handle "Save Your Login Info?" pop-up
try:
save_info_popup = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Not Now')]"))
)
save_info_popup.click()
except:
print("Save Info pop-up not found.")
# Handle "Turn on Notifications" pop-up
try:
notification_popup = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Not Now')]"))
)
notification_popup.click()
except:
print("Notification pop-up not found.")
π§βπ» Step 2: Navigating to the Profile Section
Once logged in, you can navigate to the profile section to view user information.
π Code to Navigate to Profile:
# Click on the profile icon
profile_icon = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//span[@aria-label='Profile']"))
)
profile_icon.click()
π§βπ» Step 3: Liking a Post
Let's navigate to a post and like it.
π Code to Like a Post:
# Navigate to the first post
first_post = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "(//div[@role='button'])[1]"))
)
first_post.click()
# Like the post
like_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//span[@aria-label='Like']"))
)
like_button.click()
π§βπ» Step 4: Posting a Comment
Next, we'll learn how to post a comment on a post.
π Code to Post a Comment:
# Click on the comment field
comment_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//textarea[@aria-label='Add a commentβ¦']"))
)
comment_field.click()
comment_field.send_keys("Nice post! π")
# Click the post button
post_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Post')]"))
)
post_button.click()
π― Full Code for Today's Lesson:
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
# Setup Chrome options
chrome_options = Options()
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--start-maximized")
# Initialize the driver
service = Service("chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)
# Open Instagram
driver.get("https://www.instagram.com/")
# Explicit wait for the username field
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']")))
# Enter credentials
username_field.send_keys("your_dummy_username")
password_field.send_keys("your_dummy_password")
login_button.click()
# Handle "Save Your Login Info?" pop-up
try:
save_info_popup = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Not Now')]"))
)
save_info_popup.click()
except:
print("Save Info pop-up not found.")
# Handle "Turn on Notifications" pop-up
try:
notification_popup = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Not Now')]"))
)
notification_popup.click()
except:
print("Notification pop-up not found.")
# Navigate to the profile section
profile_icon = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//span[@aria-label='Profile']"))
)
profile_icon.click()
# Like the first post
first_post = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "(//div[@role='button'])[1]"))
)
first_post.click()
like_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//span[@aria-label='Like']"))
)
like_button.click()
# Post a comment
comment_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//textarea[@aria-label='Add a commentβ¦']"))
)
comment_field.click()
comment_field.send_keys("Nice post! π")
post_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Post')]"))
)
post_button.click()
# Close the browser
driver.quit()
π― Task for Today:
- 1οΈβ£ Handle Instagram's pop-ups dynamically.
- 2οΈβ£ Navigate to your profile and like the first post.
- 3οΈβ£ Post a comment on a post.
Would you like me to explain how to scrape Instagram data next? π