-
Notifications
You must be signed in to change notification settings - Fork 0
/
like_cmt_follow.py
84 lines (68 loc) · 2.81 KB
/
like_cmt_follow.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
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import random
import time
def sleep_for_period_of_time():
limit = random.randint(7,10)
time.sleep(limit)
user = input("Enter your username: ")
pwd = input("Enter your password: ")
def main():
options = webdriver.ChromeOptions()
options.add_argument("--lang=en")
browser = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
browser.get("https://www.instagram.com")
sleep_for_period_of_time()
username_input = browser.find_element_by_css_selector("input[name='username']")
password_input = browser.find_element_by_css_selector("input[name='password']")
username_input.send_keys(user)
password_input.send_keys(pwd)
sleep_for_period_of_time()
login_button = browser.find_element_by_xpath("//button[@type='submit']")
login_button.click()
sleep_for_period_of_time()
hashtag = input("Enter your hashtag name: ")
browser.get(f"https://www.instagram.com/explore/tags/{hashtag}")
sleep_for_period_of_time()
first_pic = browser.find_element_by_xpath("//div[@class='_aaq8']/div/div/div[1]/div[1]/a")
first_pic.click()
num_post = input("How many post you want to like and leave a comment: ")
cmmt = input("Type your comment: ")
sleep_for_period_of_time()
for i in range(int(num_post)):
# Like a post
like_post = browser.find_element_by_xpath("//section[@class= '_aamu _aat0']/span[1]/button")
like_post.click()
print("Liked!")
sleep_for_period_of_time()
#Comment on a post
cmmt_post = browser.find_element_by_xpath("//textarea[@class='_ablz _aaoc']")
cmmt_post.click()
cmmt_post = browser.find_element_by_xpath("//textarea[@class='_ablz _aaoc focus-visible']")
cmmt_post.send_keys(cmmt)
cmmt_post.send_keys(Keys.ENTER)
print("Commented!")
time.sleep(5)
#Follow the user of the post
follow_user = browser.find_element(By.XPATH, '//button/div/div[contains(text(), "Follow")]')
if follow_user.text == "Follow":
follow_user.click()
print("Followed!")
sleep_for_period_of_time()
else:
pass
#Move on to the next Post
next_post = browser.find_element_by_xpath("//button[@class='_abl-']//*[name()='svg' and @aria-label='Next']")
next_post.click()
print("post " + str(i) + " done!")
print("Next!")
sleep_for_period_of_time()
#Quit the Program
answer = input("The programm finished! Click on 'e' to exit.. ")
if answer.lower().startswith("e"):
browser.quit()
exit()
if __name__ == "__main__":
main()