-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstagram.py
73 lines (58 loc) · 2.67 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
from selenium import webdriver
from time import sleep
from random import choice, randrange
class Instagram:
def __init__(self, driver, url_instagram_post):
self.__driver = driver
self.__url_instagram = 'https://www.instagram.com/'
self.__url_intagram_post = url_instagram_post
# Elements of the intagram login page #xPath
self.__input_user_name = 'username' # name
self.__input_user_password = 'password' # name
# xPath
self.__btn_login = '//*[@id="loginForm"]/div/div[3]/button'
# Elements of the intagram post page #xPath
self.__input_comment = 'Ypffh' # class
self.__btn_post_comment = '#react-root > section > main > div > div.ltEKP > article > div > div.eo2As > section.sH9wk._JgwE > div > form > button'
def navigate(self):
self.__driver.get(self.__url_instagram)
sleep(2)
def login(self, user_name, password):
print("Login Intagram")
self.__driver.find_element_by_name(
self.__input_user_name).send_keys(user_name)
self.__driver.find_element_by_name(
self.__input_user_password).send_keys(password)
self.__driver.find_element_by_xpath(self.__btn_login).click()
sleep(2)
def navigate_to_post(self):
self.__driver.get(self.__url_intagram_post)
sleep(2)
def __choice_users_name_post(self, comments_list, user_number_per_comment):
comments_list = comments_list
comments = []
while len(comments) < user_number_per_comment:
commnet = choice(comments_list)
comments.append(f'@{commnet}'.strip())
comments_list.remove(commnet)
return comments
def comment(self, comments_list, user_number_per_comment, comment_number=1):
print("Comment")
comments_list_copy = comments_list [:]
for i in range(comment_number):
if len(comments_list_copy) == 0:
comments_list_copy = comments_list[:]
choiceusers_name_post = self.__choice_users_name_post(
comments_list_copy, user_number_per_comment)
self.__driver.find_element_by_class_name(
self.__input_comment).click()
for element in choiceusers_name_post:
self.__driver.find_element_by_class_name(
self.__input_comment).send_keys(f'{element} ')
sleep(2)
self.__driver.find_element_by_css_selector(
self.__btn_post_comment).click()
print(f'Comentario de numero {i+1} => {choiceusers_name_post}')
sleep(8)
self.__driver.refresh()
sleep(60+randrange(0, 9))