I've written a script in Python using Selenium to log in to Instagram and then search for some hashtag, as in #NewYorkbarbers
, and get the link of that hashtag. My script can successfully log in, click on the Not Now
button if Turn on Notifications
box shows up, and then put that hashtag in the search box, but I can't make my script initiate that search to produce result against that hashtag.
I've tried so far:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class InstagramScraper:
login_url = 'https://www.instagram.com/accounts/login/?source=auth_switcher'
def __init__(self,username,password):
self.driver = webdriver.Chrome()
self.wait = WebDriverWait(self.driver,10)
self.login(username,password)
def login(self,username,password):
self.driver.get(self.login_url)
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[name="username"]'))).send_keys(username)
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[name="password"]'))).send_keys(password)
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'button[type="submit"]'))).click()
try:
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'button.HoLwm'))).click()
except Exception:pass
def use_hashtag(self):
self.wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,'input[placeholder="Search"]'))).send_keys("#NewYorkbarbers",Keys.ENTER)
if __name__ == '__main__':
scraper = InstagramScraper('username','password')
scraper.use_hashtag()
How can I use
return
withinlogin()
method as any ideal method should have return statement within it?How can I produce the result of that hashtag search?