Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll down in an instagram pop-up frame with Selenium

I have a python script using selenium to go to a given Instagram profile and iterate over the user's followers. On the instagram website when one clicks to see the list of followers, a pop-up opens with the accounts listed (here's a screenshot of the site)

However both visually and in the html, only 12 accounts are shown. In order to see more one has to scroll down, so I tried doing this with the Keys.PAGE_DOWN input.

from selenium import webdriver
from selenium.common.exceptions         import TimeoutException
from selenium.webdriver.support.ui      import WebDriverWait 
from selenium.webdriver.support         import expected_conditions as EC
from selenium.webdriver.chrome.options  import Options
from selenium.webdriver.common.keys     import Keys
import time 

...

username = 'Username'
password = 'Password'
message  = 'blahblah'
tryTime  = 2

#create driver and log in
driver = webdriver.Chrome()
logIn(driver, username, password, tryTime)

#gets rid of preference pop-up
a = driver.find_elements_by_class_name("HoLwm")
a[0].click()

#go to profile
driver.get("https://www.instagram.com/{}/".format(username))

#go to followers list
followers = driver.find_element_by_xpath("//a[@href='/{}/followers/']".format(username))
followers.click()
time.sleep(tryTime) 

#find all li elements in list
fBody  = driver.find_element_by_xpath("//div[@role='dialog']")
fBody.send_keys(Keys.PAGE_DOWN) 

fList  = fBody.find_elements_by_tag("li")
print("fList len is {}".format(len(fList)))

time.sleep(tryTime)

print("ended")
driver.quit()

When I try to run this I get the following error:

Message: unknown error: cannot focus element

I know this is probably because I'm using the wrong element for fBody, but I don't know which would be the right one. Does anybody know which element I should send the PAGE_DOWN key to, or if there is another way to load the accounts?

Any help is much appreciated!

like image 844
hiMom Avatar asked Dec 11 '22 04:12

hiMom


1 Answers

the element you're looking is //div[@class='isgrP'] and Keys.PAGE_DOWN is not work for scrollable div.

Your variable fList hold old value, you need to find again the elements after scroll.

#find all li elements in list
fBody  = driver.find_element_by_xpath("//div[@class='isgrP']")
scroll = 0
while scroll < 5: # scroll 5 times
    driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
    time.sleep(tryTime)
    scroll += 1

fList  = driver.find_elements_by_xpath("//div[@class='isgrP']//li")
print("fList len is {}".format(len(fList)))

print("ended")
#driver.quit()
like image 119
ewwink Avatar answered Dec 21 '22 22:12

ewwink