-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
235 lines (184 loc) · 9.06 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import time
import random
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import traceback
def xpath_soup(element):
components = []
child = element if element.name else element.parent
for parent in child.parents:
siblings = parent.find_all(child.name, recursive=False)
components.append(
child.name
if siblings == [child] else
'%s[%d]' % (child.name, 1 + siblings.index(child))
)
child = parent
components.reverse()
return '/%s' % '/'.join(components)
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument("--disable-notifications")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),
options=options) # version_main allows to specify your chrome version instead of following chrome global version
driver.maximize_window()
driver.get("https://www.facebook.com/")
time.sleep(2)
fb_group_link = input("Enter the group link : ")
choose = input("Enter \"y\" after logging in : ")
# #######
# limit_to_sent_invitation = 500
# #######
if choose == "y":
while True:
select_followers_to_be_sent = random.randint(5, 10)
error_occured = False
now_selected = 0
driver.get(fb_group_link)
count_find_or_not = 0
while True:
invite_friends_button_xpath = None
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
arr_button = soup.find_all("div", attrs={"role": "button"})
for ele_arr_button in arr_button:
try:
print(ele_arr_button.attrs["aria-label"])
if "Inviter" in ele_arr_button.attrs["aria-label"]:
invite_friends_button_xpath = ele_arr_button
break
except:
pass
if invite_friends_button_xpath is not None:
break
count_find_or_not = count_find_or_not + 1
if count_find_or_not == 10:
break
time.sleep(1)
print("invite_friends_button_xpath : ", invite_friends_button_xpath)
driver.find_element(By.XPATH, xpath_soup(invite_friends_button_xpath)).click()
count_find_or_not = 0
while True:
invite_friends_menu_button_xpath = None
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
arr_menu_button = soup.find_all("div", attrs={"role": "menuitem"})
for ele_arr_menu_button in arr_menu_button:
try:
print(ele_arr_menu_button.text)
if "Inviter des amis Facebook" in ele_arr_menu_button.text:
invite_friends_menu_button_xpath = ele_arr_menu_button
break
except:
pass
if invite_friends_menu_button_xpath is not None:
break
count_find_or_not = count_find_or_not + 1
if count_find_or_not == 10:
break
time.sleep(1)
driver.find_element(By.XPATH, xpath_soup(invite_friends_menu_button_xpath)).click()
count_find_or_not = 0
while True:
invite_friends_dialogue = None
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
invite_friends_dialogue = soup.find("div", attrs={"aria-label": "Invitez des amis à rejoindre ce groupe"})
if invite_friends_dialogue is not None:
break
count_find_or_not = count_find_or_not + 1
if count_find_or_not == 10:
break
time.sleep(1)
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
invite_friends_dialogue = soup.find("div", attrs={"aria-label": "Invitez des amis à rejoindre ce groupe"})
invite_friends_list_first_item = invite_friends_dialogue.find_all("div", attrs={"style": "padding-left: 8px; padding-right: 8px;"})
length_friends = len(invite_friends_list_first_item)
i = 0
while i < length_friends:
try:
targeted_friend = invite_friends_list_first_item[i]
print(targeted_friend)
if targeted_friend.find("div", attrs= {"role": "checkbox"}) :
element = driver.find_element(By.XPATH, xpath_soup(targeted_friend))
driver.execute_script("arguments[0].scrollIntoView()", element)
driver.find_element(By.XPATH, xpath_soup(targeted_friend)).click()
now_selected = now_selected + 1
print("Now is going on => "+str(i+1)+ " out of "+str(select_followers_to_be_sent))
if i+1 == select_followers_to_be_sent:
break
i = i + 1
time.sleep(1)
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
invite_friends_dialogue = soup.find("div", attrs={"aria-label": "Invitez des amis à rejoindre ce groupe"})
invite_friends_list_first_item = invite_friends_dialogue.find_all("div",
attrs={"style": "padding-left: 8px; padding-right: 8px;"})
length_friends = len(invite_friends_list_first_item)
print("Now length of current list => "+ str(length_friends))
print("=====================================")
else:
i = i + 1
time.sleep(1)
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
invite_friends_dialogue = soup.find("div", attrs={"aria-label": "Invitez des amis à rejoindre ce groupe"})
invite_friends_list_first_item = invite_friends_dialogue.find_all("div",
attrs={"style": "padding-left: 8px; padding-right: 8px;"})
length_friends = len(invite_friends_list_first_item)
print("I am in else")
# print("Now length of current list => "+ str(length_friends))
# print("=====================================")
except Exception:
traceback.print_exc()
error_occured = True
# if i+1 == limit_to_sent_invitation:
# break
i = i + 1
time.sleep(1)
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
invite_friends_dialogue = soup.find("div", attrs={"aria-label": "Invitez des amis à rejoindre ce groupe"})
invite_friends_list_first_item = invite_friends_dialogue.find_all("div",
attrs={"style": "padding-left: 8px; padding-right: 8px;"})
length_friends = len(invite_friends_list_first_item)
print("Error Occurs")
print("=====================================")
break
print("Now")
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
invite_friends_dialogue = soup.find("div", attrs={"aria-label": "Invitez des amis à rejoindre ce groupe"})
selected_friends = invite_friends_dialogue.find_all("span", attrs={"dir": "auto"})
for selected_friend in selected_friends:
if "AMIS SÉLECTIONNÉS" in selected_friend.text:
print(selected_friend.text)
arr_selected_invite = selected_friend.text.split(" ")
number = arr_selected_invite[0]
print(number)
print("=====================================")
break
now_selected = int(number)
print("Now selected => "+str(now_selected))
if now_selected == 0:
break
# print("now_selected => " + str(now_selected))
# print("=====================================")
# if error_occured and now_selected == 0:
# print("error_occured and now_selected => " + str(now_selected))
# print("Finished")
# break
#
# if now_selected == 0:
# print("now_selected => " + str(now_selected))
# print("Finished")
# break
html = driver.page_source
soup = BeautifulSoup(html, features="html.parser")
find_all_btns = soup.find("div", attrs={"aria-label": "Envoyer les invitations"})
driver.find_element(By.XPATH, xpath_soup(find_all_btns)).click()
time.sleep(6)