问题
Hi :) I was looking for a solution to scroll down the following/followers list in the Instagram Box. The step I do are the following:
- open an IG profile of the user A;
- click on the button "followers";
- a box with a list of 12 followers appears in a IG box.
Once the followers list is shown up, when I scroll down using the line:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
I get that the page below the box scrolls down :(
How can scroll down the list in the followers box?
Thanks in advance :) Maria.
回答1:
you can try execute_script()
and change .isgrP
if different class
...
from selenium.webdriver.support.ui import WebDriverWait
.....
# after click follower link, wait until dialog appear
WebDriverWait(driver, 10).until(lambda d: d.find_element_by_css_selector('div[role="dialog"]'))
# now scroll
driver.execute_script('''
var fDialog = document.querySelector('div[role="dialog"] .isgrP');
fDialog.scrollTop = fDialog.scrollHeight
''')
回答2:
This method works perfectly for my situation. Don't change the sleep times within the loop please. They allow reload of followers/following within the dialogue box without having to scroll back up.
FList = driver.find_element_by_css_selector('div[role=\'dialog\'] ul')
numberOfFollowersInList = len(FList.find_elements_by_css_selector('li'))
FList.click()
actionChain = webdriver.ActionChains(driver)
time.sleep(random.randint(2,4))
while (numberOfFollowersInList < max):
actionChain.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
numberOfFollowersInList = len(FList.find_elements_by_css_selector('li'))
time.sleep(0.4)
print(numberOfFollowersInList)
actionChain.key_down(Keys.SPACE).key_up(Keys.SPACE).perform()
time.sleep(1)
来源:https://stackoverflow.com/questions/53681446/scroll-down-followers-following-list-in-the-instagram-box