如何使用Python的Selenium滚动Instagram Bot中的子弹窗
Hey there! I’ve dealt with this exact problem when building Instagram bots before—those modals (like follower/following lists, message threads) have their own independent scroll containers, so scrolling the main window won’t touch them. Let’s break down how to fix it:
Step 1: Locate the Modal’s Scrollable Container
First, you need to identify the actual DOM element that handles scrolling for the modal. On Instagram, these modals typically use a <div> with a specific class (it might change over time, but right now common ones are _aano for follower lists).
To find it:
- Open your browser’s dev tools (F12), go to the Elements tab.
- Right-click inside the scrollable area of the modal and select "Inspect".
- Look for the parent element that has
overflow: autooroverflow: scrollin its CSS—this is your scroll container.
Step 2: Scroll the Target Element Directly with JavaScript
Instead of scrolling the main window, target the scrollable container element you found. Here’s how to do it in your code:
Basic Scroll by Pixels
from selenium.webdriver.common.by import By # Locate the scrollable modal element scrollable_modal = driver.find_element(By.CLASS_NAME, "_aano") # Scroll down by 500 pixels within the modal driver.execute_script("arguments[0].scrollBy(0, 500);", scrollable_modal)
Scroll to the Bottom of the Modal
If you want to scroll all the way to the end (e.g., loading all followers), use this:
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight;", scrollable_modal)
Step 3: Add Wait to Ensure the Modal Loads
Sometimes the modal might not be fully loaded when you try to interact with it. Use explicit waits to avoid errors:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Wait up to 10 seconds for the scrollable container to appear scrollable_modal = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CLASS_NAME, "_aano")) ) # Now perform the scroll driver.execute_script("arguments[0].scrollBy(0, 500);", scrollable_modal)
Why Your Original Code Didn’t Work
The window.scrollBy() command only affects the main browser window’s scroll area. Instagram’s modals are nested elements with their own scroll context, so you have to target that specific container instead of the global window.
内容的提问来源于stack exchange,提问作者Khaqan Ahmed

