2

Edit: The suggested solution would work for regular web scrolling. However, I'm not referring to the main webpage. That solution does not work for a Javascript overlay.

I've successfully authenticated to Instagram, navigated to the profile home page and click()'d the followers button. However, I haven't found any solution on Stack Overflow/GTS'ing to scrolling down to the bottom of the javascript overlay. Does anyone have a solution to this?

Also, as this is my first real project in Python, I am to NOT USE any pre-built APIs. I know this is possible without them, I just don't know how to do it.

Here's my current code:

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def login(driver):
    #Place credentials for use here
    username = ""  # <username here>
    password = ""  # <password here>
    userid = ""

    # Load page
    driver.get("https://www.instagram.com/accounts/login/")
    # Login
    driver.find_element_by_xpath("//div/input[@name='username']").send_keys(username)
    driver.find_element_by_xpath("//div/input[@name='password']").send_keys(password)
    driver.find_element_by_xpath("//span/button").click()
    # Wait for the login page to load
    WebDriverWait(driver, 2).until(
        EC.presence_of_element_located((By.XPATH, './/div/a[@href="/{0}/"]'.format(userid))))

def load_followers(driver):
    userid = "" #user ID goes here
    driver.get("https://www.instagram.com/{0}/".format(userid))

    try:
        #click "Followers" via xpath to open JS Overlay
        loc = driver.find_element_by_xpath("//li//a").click()
    except Exception as e:
        driver.quit()
        print e

    try:
        print "\nTrying to go into new window..."
        print "Trying to scroll....\n"
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
    except Exception as e:
        print e

driver = webdriver.Chrome()

try:
    login(driver)
    load_followers(driver)
finally:
    driver.quit()

I would greatly appreciate any help!

0 Answers0