-
Notifications
You must be signed in to change notification settings - Fork 0
/
instagram.py
110 lines (74 loc) · 3.98 KB
/
instagram.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.support import expected_conditions as EC, ui
from selenium.webdriver.support.wait import WebDriverWait
class Instagram:
def __init__(self):
self.driver = webdriver.Chrome(executable_path='C:\Drivers\chromedriver.exe')
self.driver.maximize_window()
self.driver.get('https://www.instagram.com/accounts/#/?source=auth_switcher')
print(self.driver.title)
# self.assertEqual('Instagram',title,'Not Instagram')
def login(self,u,p):
username = WebDriverWait(self.driver,10).until(EC.element_to_be_clickable((By.NAME,'username')))
username.clear()
username.send_keys(u)
sleep(1)
password = WebDriverWait(self.driver,10).until(EC.element_to_be_clickable((By.NAME,'password')))
password.clear()
password.send_keys(p)
logit = self.driver.find_element_by_tag_name('form')
logit.submit()
def goToHome(self):
homebtn = WebDriverWait(self.driver,10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[1]/div/a')))
homebtn.click()
ui.WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.HoLwm"))).click()
def followFromUser(self,name,numOfFollowers):
self.driver.get('https://www.instagram.com/'+name+'/')
followingBtn = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a')))
followingBtn.click()
sleep(3)
for i in range(1, numOfFollowers+1):
followpeople = self.driver.find_element_by_xpath(
'/html/body/div[4]/div/div/div[2]/ul/div/li[' + str(i) + ']/div/div[3]/button')
followpeople.click()
sleep(0.5)
sleep(1)
self.driver.find_element_by_xpath('/html/body/div[4]/div/div/div[1]/div/div[2]/button').click()
def unfollowFollowing(self,numOfUnfollow):
navprofileXpath = '//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[5]/span/img'
navprofile = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, navprofileXpath)))
navprofile.click()
sleep(1)
profile = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[5]/div[2]/div/div[2]/div[2]/a[1]')
profile.click()
myfollowing = WebDriverWait(self.driver,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="react-root"]/section/main/div/header/section/ul/li[3]/a')))
myfollowing.click()
for i in range(1,numOfUnfollow + 1):
unfollowbtn = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(
(By.XPATH, '/html/body/div[4]/div/div/div[2]/ul/div/li['+str(i)+']/div/div[3]/button')))
unfollowbtn.click()
finalUnfollowBtn = WebDriverWait(self.driver,10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[5]/div/div/div/div[3]/button[1]')))
finalUnfollowBtn.click()
sleep(1)
closeBtn = self.driver.find_element_by_xpath('/html/body/div[4]/div/div/div[1]/div/div[2]/button')
closeBtn.click()
self.goToHome()
def logout(self):
navprofileXpath = '//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[5]/span/img'
navprofile = WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, navprofileXpath)))
navprofile.click()
sleep(1)
logout = self.driver.find_element_by_xpath(
'//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[5]/div[2]/div/div[2]/div[2]/div')
logout.click()
i = Instagram()
# sleep(5)
i.login('your Username','your password')
sleep(3)
i.unfollowFollowing(2)
i.followFromUser('account to follow user from',5)
sleep(2)
i.logout()