-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay52.py
More file actions
84 lines (68 loc) · 2.96 KB
/
Day52.py
File metadata and controls
84 lines (68 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import time
from dotenv import load_dotenv
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
load_dotenv()
SIMILAR_ACCOUNT = "python"
USERNAME = os.getenv("YOUR_USERNAME")
PASSWORD = os.getenv("YOUR_PASSWORD")
print("Day 52 - 100 Days of Code.")
print("Welcome to Instagram Follower Bot.")
class InstaFollower:
def __init__(self):
options = webdriver.ChromeOptions()
options.add_experimental_option(name="detach", value=True)
self.driver = webdriver.Chrome(options=options)
def login(self):
"""Logs into Instagram."""
self.driver.get("https://www.instagram.com/accounts/login/")
time.sleep(4)
# Handle cookie warning if present
cookie_warning = self._find_element(by=By.XPATH,
value="/html/body/div[6]/div[1]/div/div[2]/div/div/div/div/div["
"2]/div/button[2]")
if cookie_warning:
cookie_warning.click()
self._find_element(By.NAME, "username").send_keys(USERNAME)
password_field = self._find_element(by=By.NAME, value="password")
password_field.send_keys(PASSWORD)
time.sleep(2)
password_field.send_keys(Keys.ENTER)
time.sleep(4)
# Handle prompts
self._click_if_present(by=By.XPATH, value="//div[contains(text(), 'Not now')]")
time.sleep(3)
self._click_if_present(by=By.XPATH, value="//button[contains(text(), 'Not Now')]")
def find_followers(self):
"""Navigates to the followers list of a similar account."""
self.driver.get(f"https://www.instagram.com/{SIMILAR_ACCOUNT}/followers")
time.sleep(8)
modal = self._find_element(by=By.XPATH,
value="/html/body/div[6]/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[2]")
for _ in range(5):
self.driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", modal)
time.sleep(2)
def follow(self):
"""Follows users from the followers list."""
buttons = self.driver.find_elements(by=By.CSS_SELECTOR, value='._aano button')
for button in buttons:
try:
button.click()
time.sleep(1)
except ElementClickInterceptedException:
self._click_if_present(by=By.XPATH, value="//button[contains(text(), 'Cancel')]")
def _find_element(self, by, value):
"""Helper to find an element."""
return self.driver.find_element(by=by, value=value)
def _click_if_present(self, by, value):
"""Helper to click an element if it exists."""
element = self.driver.find_elements(by=by, value=value)
if element:
element[0].click()
bot = InstaFollower()
bot.login()
bot.find_followers()
bot.follow()