-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.py
More file actions
69 lines (56 loc) · 2.58 KB
/
task2.py
File metadata and controls
69 lines (56 loc) · 2.58 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 selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time
#edit username password here before running the bot
username = "zatchssj2"
password = "somepassword"
#initializing wait and driver
driver = webdriver.Firefox()
driver.maximize_window()
wait = WebDriverWait(driver,5)
def check_100(item):
try:
driver.find_element_by_css_selector(item)
except NoSuchElementException:
return False
return True
#getting login website
driver.get("https://www.instagram.com/accounts/login/")
#page wait to ensure variable has been loaded
page_wait = wait.until(EC.presence_of_element_located(
(By.CLASS_NAME, "KPnG0")))
#login
user = driver.find_element_by_name('username')
user.clear()
user.send_keys(username)
pas = driver.find_element_by_name('password')
pas.clear()
pas.send_keys(password)
#page wait to ensure login button is present
page_wait = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[4]')))
driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[4]/button').click()
time.sleep(3)
#once logged in opening the other page
driver.get("https://www.instagram.com/emmawatson")
page_wait = wait.until(EC.element_to_be_clickable((By.XPATH,'//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a')))
driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a').click()
#scrolling down the pop to find the 100th user
while True:
time.sleep(1)
popup = driver.find_element_by_xpath('/html/body/div[3]/div//a')
check = check_100("li.wo9IH:nth-child(100)")
if check == True:
break
popup.send_keys(Keys.END)
#after checking for 100th user, click on the user link
user_100 = driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/ul/div/li[100]/div/div[1]/div[2]/div[1]/a").click()
#there can 2 xpaths depeneding on the type of instagram page so we try for both
try:
page_wait = wait.until(EC.element_to_be_clickable((By.XPATH,'/html/body/span/section/main/div/header/section/div[1]/div[1]/span/span[1]/button')))
driver.find_element_by_xpath("/html/body/span/section/main/div/header/section/div[1]/div[1]/span/span[1]/button").click()
except:
driver.find_element_by_xpath("/html/body/span/section/main/div/header/section/div[1]/button").click()