Search code examples
pythonseleniumselenium-webdriverscroll

How to Scroll the Left Quadrant


How can I make Selenium run scroll only in the left quadrant?

when I use the command below it is executed in the zoom of the map and that is not my intention, because I want to scrape the links of the companies that are in the left column

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

enter image description here


Solution

  • You need to find the scrollable div element and then you can apply JavaScript as following:

    element = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@role='main']//div[contains(@aria-label,'lanchonet')]")))
    driver.execute_script("arguments[0].scroll(0, arguments[0].scrollHeight);", element)
    

    The code above works for me.
    The entire code is:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument("start-maximized")
    
    webdriver_service = Service('C:\webdrivers\chromedriver.exe')
    driver = webdriver.Chrome(options=options, service=webdriver_service)
    wait = WebDriverWait(driver, 5)
    
    url = "https://www.google.com.br/maps/search/lanchonete,/@-27.0027727,-48.6293259,15z"
    driver.get(url)
    
    element = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@role='main']//div[contains(@aria-label,'lanchonet')]")))
    driver.execute_script("arguments[0].scroll(0, arguments[0].scrollHeight);", element)
    

    you can, of course, scroll for other lengths, not only for the entire height.