3

i try to scrolldown on a website using selenium with the following code: (i would like to scroll down on the left side of the website where you can see the entries)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
import time

link = "https://www.bing.com/maps/search?style=r&q=auto+IN+Vienna&srs=sb&cp=48.209085%7E16.392537&lvl=12.2"

options = Options()
options.add_argument("start-maximized")  
srv=Service()
driver = webdriver.Chrome (service=srv, options=options)
driver.get (link)
time.sleep(3)

driver.execute_script("window.scrollBy(0, 10000)")
input("Press!")

But it simply stay on the very top after running the code:

enter image description here

How can i scroll down using selenium to get more results on this site?

2 Answers 2

3

The scrollbars belong to a scrollable container element (not window/document), so window.scrollBy(0, 10000) does nothing.

You must target the element that actually scrolls and that element is <div class="b_lstcards">

enter image description here

get the scrollbar element:

scroll_bar = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.b_lstcards')))

and then apply the scrolling to get to the bottom of it:

driver.execute_script("arguments[0].scrollBy(0, arguments[0].scrollHeight);", scroll_bar)

here's the sample code:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

link = "https://www.bing.com/maps/search?style=r&q=auto+IN+Vienna&srs=sb&cp=48.206547%7E16.381167&lvl=12.5"

options = Options()
options.add_argument("start-maximized")

driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 10)

driver.get(link)

scroll_bar = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div.b_lstcards')))

driver.execute_script("arguments[0].scrollBy(0, arguments[0].scrollHeight);", scroll_bar)

time.sleep(5)
Sign up to request clarification or add additional context in comments.

1 Comment

Work like a charm - thank you so much!
2

driver.execute_script("window.scrollBy(0, -30000)")

Negative values scroll up.

driver.execute_script("window.scrollBy(0, 1000)")

... should scroll down.

Also note, if you select an element lower down on the page, it will be automatically scrolled to (unless it's dynamically generated and doesn't exist yet).

2 Comments

Thanks - i changed the scroll amount to 10000 - but it is still not working and the data on the left is not scrolled down (i will correct the minus to plus in the initial question)
you probably need to switch to a different frame

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.