Home > Software design >  Can't scroll down in web page
Can't scroll down in web page

Time:09-11

In python and selenium I can't scroll down in this page https://melkemun.com/ to show & get another post

driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")

CodePudding user response:

This page uses body to crop displayed area and real scrolled area is

<div  onscroll="loadMore(event)">

This works for me in JavaScript console (in DevTools in Firefox)

item = document.getElementsByClassName('scroll-body')[0];
item.scrollTo(0, item.scrollHeight);

so you may need

driver.execute_script("item=document.getElementsByClassName('scroll-body')[0];item.scrollTo(0,item.scrollHeight);")

Page uses Shadow Root and code works only with Chrome. (Selenium: 4.4.0)

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

from webdriver_manager.chrome import ChromeDriverManager
#from webdriver_manager.firefox import GeckoDriverManager

import time

url = 'https://melkemun.com/'

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
#driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))

driver.get(url)

print('sleep')
time.sleep(5)

shadow = driver.find_element(By.ID, 'stting').shadow_root
#print(shadow)

# select city
shadow.find_elements(By.CLASS_NAME, "btn-city")[0].click()

print('sleep')
time.sleep(5)

for i in range(5):
    print('scrolling:', i)
    #driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
    driver.execute_script('item=document.getElementsByClassName("scroll-body")[0];item.scrollTo(0,item.scrollHeight);')
    print('sleep')
    time.sleep(5)
  • Related