D
D
dancer_and_programmer2020-06-04 10:44:07
Python
dancer_and_programmer, 2020-06-04 10:44:07

How to scroll page up Selenium Python?

Good afternoon everyone. I am writing a Python script that parses the number of publications from this page: https://yandex.ru/chat/#/chats/1%2F0%2Fccb05ef5-14...

But the parser sees only the first 6 publications, you need to scroll the page up. Added a line:

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


But it still doesn't work, still 6 publications. Can you tell me how to make the script scroll up and parse the number of ALL posts on this channel?*

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup


list_with_link = ['https://yandex.ru/chat/#/chats/1%2F0%2Fccb05ef5-1472-4e50-a926-602807a6ef94']
driver = webdriver.Chrome()
driver.implicitly_wait(10)
for url in list_with_link:
    driver.get(url)

    driver.execute_script("window.scrollTo(document.body.scrollHeight, 0);")
    count = 0
    html = driver.page_source
    soup = BeautifulSoup(html, 'lxml')
    count_of_publ = soup.find_all('div', class_='yamb-message-row')

    for text in count_of_publ:
        count += 1
        
    print('Количество публикаций:', count)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
coderisimo, 2020-06-04
@dancer_and_programmer

General idea. You need to turn the mouse wheel (scroll) through JS.
Put something like this in driver.execute_script :

var evt = document.createEvent('MouseEvents');
evt.initEvent('wheel', true, true); 
evt.deltaY = -100000; 
document.querySelector('.yamb-conversation__content').dispatchEvent(evt);

instead of 10000 you can use window.innerHeight.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question