如何使用Python Selenium触发分页滚动事件直接跳转到指定页码?
Absolutely! You can skip the tedious process of clicking through pages one by one and jump straight to page 100 using Selenium—here's how to do it, based on the pagination code snippet you shared:
Looking at your pagination buttons, each uses Event.fire() (from Prototype.js, it appears) to trigger the rich:datascroller:onscroll event with the target page number. Since page 100 won’t be rendered in the visible button bar, clicking a physical button isn’t feasible. Instead, we can directly execute the same JavaScript event that the buttons use.
Example Python Selenium Code:
from selenium import webdriver # Initialize your browser driver (Chrome used here; adjust for Firefox/Edge as needed) driver = webdriver.Chrome() driver.get("your_target_page_url") # Execute the JavaScript to trigger the page jump driver.execute_script(""" # Locate the parent table of the pagination buttons (the element where the event is bound) const scrollerContainer = document.querySelector('.rich-datascr-inact').closest('table'); # Fire the scroll event to jump to page 100 Event.fire(scrollerContainer, 'rich:datascroller:onscroll', {'page': '100'}); """)
Alternative: Standard Custom Event
If for some reason Event.fire() isn’t available in your page context (unlikely given your snippet), you can create and dispatch a standard custom event instead:
driver.execute_script(""" const scrollerContainer = document.querySelector('.rich-datascr-inact').closest('table'); const scrollEvent = new CustomEvent('rich:datascroller:onscroll', { detail: {'page': '100'} }); scrollerContainer.dispatchEvent(scrollEvent); """)
Quick side note: If your application supports page navigation via URL parameters (e.g., ?page=100 appended to the URL), this is even faster:
current_url = driver.current_url # Replace existing page parameter or add it to the base URL new_url = current_url.split('?')[0] + '?page=100' driver.get(new_url)
Just confirm your app handles URL-based page jumps before using this method!
内容的提问来源于stack exchange,提问作者daniboy000

