Winterflags Winterflags - 8 months ago 41
Python Question

Selenium: Dynamically loaded page causes automatic scrolling to fail

I'm using

for Python to navigate a dynamic webpage (link).

I would like to automatically scroll down to a y-point on the page. I have tried various ways to code this, which seem to work on other pages, but not on this particular webpage.

Some examples of my efforts that don't seem to work for Firefox with Selenium:

from selenium import webdriver

driver = webdriver.Firefox()

By scrolling to a y-position:

driver.execute_script("window.scrollTo(0, 1400);")

Or by finding an element:

find_scrollpoint = driver.find_element_by_xpath("//*[@id='main-wrapper']/div[10]/div/div[1]/div[1]")
scrollpoint = find_scrollpoint.location["y"]
driver.execute_script("window.scrollTo(0, scrollpoint);")


What could be some unusual circumstances on webpages such as this, where this very typical
code may fail?

What can possibly be done to mitigate it? Perhaps keypresses to scroll down – but it would still have to know when to stop pressing down.


It is just quite a dynamic page. I would wait for this specific element to be visible and then scroll into it's view. This works for me:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from import WebDriverWait
from import expected_conditions as EC
from import By

driver = webdriver.Firefox()

wait = WebDriverWait(driver, 10)
elm = wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@id='main-wrapper']/div[10]/div/div[1]/div[1]")))

driver.execute_script("arguments[0].scrollIntoView();", elm)