Home > Enterprise >  Why is scrolling page down in selenium not working?
Why is scrolling page down in selenium not working?

Time:06-20

I have the problem with scroll page with selenium and, to be precise, with scrolling to the bottom of the page. This two line of code does not work for me. Who can explain, why?:

browser.execute_script("window.scrollBy(0, document.body.scrollHeight)")
    
browser.execute_script("window.scrollTo(0, document.body.scrollHeight)")   

This is my properties for WebDriver:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

But this options works, but I need to scroll page without connect to some element:

flag = browser.find_element(By.XPATH, ".....")
browser.execute_script("arguments[0].scrollIntoView();", flag)

CodePudding user response:

You can find the body and try:

body = driver.find_element_by_xpath('/html/body')
body.send_keys(Keys.PAGE_DOWN)

You can find more information, e.g. here: enter link description here

  • Related