Select a Subtopic
π― Day 16: Connecting Tkinter GUI to Your Instagram Bot (Selenium)
Today, we will connect the Tkinter GUI you created on Day 15 to a Selenium bot. The GUI will accept username and password inputs, and upon clicking the Start Bot button, it will log in to Instagram using Selenium.
β What Youβll Learn Today:
- Setting up Selenium and WebDriver.
- Connecting the Tkinter GUI to the bot.
- Using event handling to start the bot.
- Creating a simple Instagram login bot.
π 1. Installing Selenium
First, you need to install Selenium if you havenβt already:
pip install selenium
Also, download the Chrome WebDriver that matches your Chrome version from:
https://sites.google.com/chromium.org/driver/π₯ 2. Basic Instagram Login Bot Code (Selenium)
Hereβs a simple Selenium bot to log in to Instagram:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
def instagram_login(username, password):
# Set up the WebDriver
driver = webdriver.Chrome(executable_path="path/to/chromedriver")
# Go to Instagram login page
driver.get("https://www.instagram.com/accounts/login/")
# Wait for the page to load
time.sleep(5)
# Enter username
driver.find_element(By.NAME, "username").send_keys(username)
# Enter password
driver.find_element(By.NAME, "password").send_keys(password)
# Click the login button
driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button').click()
# Wait to see the result
time.sleep(10)
# Close the browser
driver.quit()π 3. Integrating Tkinter GUI with Selenium
Now, letβs connect the Tkinter GUI to the Instagram bot.
import tkinter as tk
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Function to log in to Instagram
def instagram_login(username, password):
driver = webdriver.Chrome(executable_path="path/to/chromedriver")
driver.get("https://www.instagram.com/accounts/login/")
time.sleep(5)
driver.find_element(By.NAME, "username").send_keys(username)
driver.find_element(By.NAME, "password").send_keys(password)
driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button').click()
time.sleep(10)
driver.quit()
# Function to start the bot
def start_bot(username, password):
print(f"Starting bot with Username: {username} and Password: {password}")
instagram_login(username, password)
# Create the main window
root = tk.Tk()
root.title("Instagram Bot")
root.geometry("400x300")
# Create labels and entry fields
username_label = tk.Label(root, text="Username:")
username_label.grid(row=0, column=0, padx=10, pady=10)
username_entry = tk.Entry(root)
username_entry.grid(row=0, column=1, padx=10, pady=10)
password_label = tk.Label(root, text="Password:")
password_label.grid(row=1, column=0, padx=10, pady=10)
password_entry = tk.Entry(root, show="*")
password_entry.grid(row=1, column=1, padx=10, pady=10)
# Create a button to start the bot
start_button = tk.Button(root, text="Start Bot", command=lambda: start_bot(username_entry.get(), password_entry.get()))
start_button.grid(row=2, column=0, columnspan=2, pady=20)
# Run the main event loop
root.mainloop()π§βπ» 4. Step-by-Step Guide:
- Save the script as instagram_bot_gui.py.
- Replace path/to/chromedriver with the actual path to your ChromeDriver.
- Run the script:
python instagram_bot_gui.py
- Enter your Instagram credentials in the GUI and click Start Bot.
- The bot will log in to Instagram automatically using Selenium.
π¨ 5. Optional: Adding Loading Messages
You can add a loading message in the GUI to show when the bot starts.
def start_bot(username, password):
status_label.config(text="Bot is running...")
instagram_login(username, password)
status_label.config(text="Bot finished.")Add the status label in the GUI:
status_label = tk.Label(root, text="", fg="green") status_label.grid(row=3, column=0, columnspan=2)
π 6. Optional: Masking Password Input
In Tkinter, you can mask the password field by setting show="*" in the Entry widget:
password_entry = tk.Entry(root, show="*")
π― 7. Full Code:
import tkinter as tk
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Function to log in to Instagram
def instagram_login(username, password):
driver = webdriver.Chrome(executable_path="path/to/chromedriver")
driver.get("https://www.instagram.com/accounts/login/")
time.sleep(5)
driver.find_element(By.NAME, "username").send_keys(username)
driver.find_element(By.NAME, "password").send_keys(password)
driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button').click()
time.sleep(10)
driver.quit()
# Function to start the bot
def start_bot(username, password):
status_label.config(text="Bot is running...")
instagram_login(username, password)
status_label.config(text="Bot finished.")
# Create the main window
root = tk.Tk()
root.title("Instagram Bot")
root.geometry("400x300")
# Create labels and entry fields
username_label = tk.Label(root, text="Username:")
username_label.grid(row=0, column=0, padx=10, pady=10)
username_entry = tk.Entry(root)
username_entry.grid(row=0, column=1, padx=10, pady=10)
password_label = tk.Label(root, text="Password:")
password_label.grid(row=1, column=0, padx=10, pady=10)
password_entry = tk.Entry(root, show="*")
password_entry.grid(row=1, column=1, padx=10, pady=10)
# Create a button to start the bot
start_button = tk.Button(root, text="Start Bot", command=lambda: start_bot(username_entry.get(), password_entry.get()))
start_button.grid(row=2, column=0, columnspan=2, pady=20)
# Status label
status_label = tk.Label(root, text="", fg="green")
status_label.grid(row=3, column=0, columnspan=2)
# Run the main event loop
root.mainloop()π Task for Today:
- Build the Instagram Bot GUI.
- Connect it to the Selenium bot.
- Test it by logging in to Instagram.
Would you like me to guide you through adding error handling for incorrect logins or improving the GUI layout in Day 17? ππ