无法实现分屏左侧窗口滚动到底部:Python+Selenium技术求助
Hey there! I’ve run into this exact problem before—trying to scroll a nested sidebar instead of the main window can be tricky because standard scroll commands target the whole page, not the specific scrollable container. Let’s break down how to fix this.
First, Identify the Root Issue
Chances are you’re using global scroll methods (like window.scrollTo()) which won’t affect the left sidebar if it’s a separate scrollable element (usually a <div> with overflow: auto or overflow: scroll CSS property). You need to target that specific container first.
Step 1: Correctly Locate the Left Scrollable Container
Use your browser’s dev tools (F12) to inspect the left sidebar and find its unique selector (CSS or XPath). For example, if the sidebar has a class like left-sidebar, you’d locate it like this:
from selenium import webdriver from selenium.webdriver.common.by import By import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() driver.get("your-website-link-here") # Replace with your actual sidebar selector left_sidebar = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, ".left-sidebar")) )
Step 2: Scroll the Sidebar Element
Once you’ve got the container, use JavaScript to scroll it—here are a few reliable methods:
Option 1: Scroll to the Bottom of the Sidebar
This will trigger any "load more" content tied to scrolling to the end:
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight;", left_sidebar)
Option 2: Scroll by a Fixed Pixel Amount
If you need to scroll incrementally (e.g., to trigger partial loads):
# Scroll down 500 pixels driver.execute_script("arguments[0].scrollBy(0, 500);", left_sidebar)
Option 3: Scroll to a Specific Element
If you know there’s a "Load More" button or target element in the sidebar, scroll directly to it:
target_element = left_sidebar.find_element(By.XPATH, "//button[text()='Load More']") driver.execute_script("arguments[0].scrollIntoView({behavior: 'smooth', block: 'end'});", target_element)
Step 3: Wait for Content to Load
After scrolling, always wait for new content to appear instead of relying solely on time.sleep() (though a short sleep can work for simple cases):
# Wait for new items to load (replace selector with your content's identifier) WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, ".new-content-item")) )
Step 4: Loop to Load All Content
If you need to keep scrolling until no new content loads, use a loop to check the scroll height:
last_scroll_height = driver.execute_script("return arguments[0].scrollHeight;", left_sidebar) while True: # Scroll to bottom driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight;", left_sidebar) # Wait for potential loading time.sleep(2) # Check new scroll height new_scroll_height = driver.execute_script("return arguments[0].scrollHeight;", left_sidebar) if new_scroll_height == last_scroll_height: # No new content loaded, exit loop break last_scroll_height = new_scroll_height
Quick Note on Your Code Snippet
I noticed your code cut off at from selenium.webdriver.common.by i...—make sure you complete that line with:
from selenium.webdriver.common.by import By
Double-check your sidebar selector too—if you’re targeting the wrong element, the scroll won’t work. Use dev tools to verify that the element you’ve selected has the overflow property set to enable scrolling.
内容的提问来源于stack exchange,提问作者SIM

