0

How would I scroll in the following window on Instagram using Selenium and Python? I've tried everything I've found and none of them work.

Here's what I have to get me to the following window:

from selenium import webdriver

browser = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
browser.implicitly_wait(5)

browser.get('https://www.instagram.com/')

sleep(2)

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("Enter Username Here")
password_input.send_keys("Enter Password Here")

login_button = browser.find_element_by_xpath("//button[@type='submit']")
login_button.click()

sleep(5)

browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div/div/section/div/button').click()

sleep(2)

browser.find_element_by_css_selector('button.aOOlW:nth-child(2)').click()

browser.get('https://www.instagram.com/instagram/')

sleep(2)

browser.find_element_by_css_selector('li.Y8-fY:nth-child(3) > a:nth-child(1) > div:nth-child(1)').click()

sleep(2)

follower_number =int( driver.find_elements_by_xpath('//span [@class="g47SY "]')[2].text)
i=0

while(i<follower_number):
    element = driver.find_elements_by_xpath("//div[@role='dialog']//ul//li")
    driver.execute_script("arguments[0].scrollIntoView(true);",element[i])```
yurd
  • 1
  • 1

1 Answers1

0

have a look at this Question. i tried that and worked.

my code

from selenium.webdriver.common.action_chains import ActionChains
import time
import re
from webdriver_manager.chrome import ChromeDriverManager
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

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://www.instagram.com/")
time.sleep(5)
my_email=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input')
my_email.send_keys("")



my_password=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label/input')
my_password.send_keys("")
time.sleep(5)

login=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]')
login.click()
time.sleep(5)
driver.get("https://www.instagram.com/instagram/following/")

driver.find_element_by_css_selector('li.Y8-fY:nth-child(3) > a:nth-child(1) > div:nth-child(1)').click()
time.sleep(5)


# this is not perfect but it will get your work done
# getting the number of followers 
follower_number  =int( driver.find_elements_by_xpath('//span [@class="g47SY "]')[2].text)
i = 0 

# looping for the exact number of followers ...
while(i<follower_number):
    # as the website is dyanamic so updating the follwers list and also the webelement 
    element = driver.find_elements_by_xpath("//div[@role='dialog']//ul//li")
    # executing scroll into view script to view the element and thats gonna load the next element(follower ) ..ultimately your scrolling achived
    driver.execute_script("arguments[0].scrollIntoView(true);",element[i])
    time.sleep(2)
    print(i)
    i=i+1


rick
  • 195
  • 7
  • But does this scroll within the popup window? – yurd Feb 12 '22 at 06:45
  • if you switch to that window it will work. have a look [here](https://stackoverflow.com/questions/10629815/how-to-switch-to-new-window-in-selenium-for-python) – rick Feb 12 '22 at 07:00
  • It's not an actual new window, its just a popup within the same window. – yurd Feb 12 '22 at 07:29
  • can you share a code or an image ? – rick Feb 12 '22 at 16:33
  • I'm not sure if you're able to try that code, but if you are, it'll take you right to the window I'm referring to. If you want to try connecting on discord or something we can. I really appreciate the help. – yurd Feb 12 '22 at 18:57
  • Hey, you still able to help me with this? – yurd Feb 14 '22 at 02:51
  • sorry for the delay .. i will look into it and asp..and here is my discord `TheNafi#2619` – rick Feb 14 '22 at 17:29
  • and lastly, I updated the code. please have look. I guess that's what you need. Also sorry for the late response. – rick Feb 14 '22 at 18:51
  • You're all good. I tried it but it's still not working. I'm pretty new so is there anything I need to change in the code above besides entering my username and password? – yurd Feb 15 '22 at 04:55
  • Aren't you trying to get the followers list ???what's your ultimate goal? What kind of error you are having? – rick Feb 15 '22 at 05:56
  • I'm trying to have it scroll to the 11th-21st person in the following list and so on because you can only do stuff with the first 10 before having to scroll. – yurd Feb 16 '22 at 03:25