-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebdriver_test.py
More file actions
69 lines (61 loc) · 2.79 KB
/
webdriver_test.py
File metadata and controls
69 lines (61 loc) · 2.79 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
from selenium import webdriver
from time import sleep
# driver = webdriver.Chrome() - For sampling testing purpose
# driver.get("https://www.google.com")
# print (driver.title)
# print (driver.current_url)
# driver.quit()
class InstaBot:
def __init__(self, username, pw):
self.driver = webdriver.Chrome()
self.username = username
self.driver.get("https://instagram.com")
sleep(2)
self.driver.find_element_by_xpath("//input[@name=\"username\"]")\
.send_keys(username)
self.driver.find_element_by_xpath("//input[@name=\"password\"]")\
.send_keys(pw)
self.driver.find_element_by_xpath('//button[@type="submit"]')\
.click()
sleep(4)
self.driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]")\
.click()
sleep(2)
def get_unfollowers(self):
self.driver.find_element_by_xpath("/html/body/div[1]/section/main/section/div[3]/div[1]/div/div[2]/div[1]/a")\
.click()
sleep(5)
self.driver.find_element_by_xpath("/html/body/div[1]/section/main/div/header/section/ul/li[3]/a")\
.click()
following = self._get_names()
self.driver.find_element_by_xpath("/html/body/div[1]/section/main/div/header/section/ul/li[2]/a")\
.click()
followers = self._get_names()
# We find the users in following but not in the followers list and catch those *******
not_following_back = [user for user in following if user not in followers]
u_arent_following = [user for user in followers if user not in following]
print("People who aren't following you back - ",not_following_back)
print()
print("People you aren't following back - ", u_arent_following)
def _get_names(self):
sleep(5)
# I made it 5 because by internet is already bad as it is.
scroll_box = self.driver.find_element_by_xpath("/html/body/div[4]/div/div[2]")
last_ht, ht = 0, 1
while last_ht != ht:
last_ht = ht
sleep(1)
#scroll to bottom of scrollbox and return the ht of the scrollbox
ht = self.driver.execute_script("""
arguments[0].scrollTo(0, arguments[0].scrollHeight);
return arguments[0].scrollHeight;
""", scroll_box)
#Converting list a tags into string format
links = scroll_box.find_elements_by_tag_name('a')
names = [name.text for name in links if name.text != '']
# close button
self.driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]")\
.click()
return names
my_bot = InstaBot('enter-your-username', 'enter-your-password')
my_bot.get_unfollowers()