0

How can we scroll down in a particular section of webpage until the end using selenium python?

Example:

Webpage : https://www.google.com/maps/search/Chemists+in+varanasi+district

On opening the page, in the left panel by default there are only 7 listings (Can be seen in inspect html also) but as we scroll down it loads more listing until all the 20 of them are listed.

Tried multiple approach listed below but none of them worked:

elem = driver.find_element_by_class_name('section-scrollbox')
(Basically getting the left pane element based on class name)
elem.send_keys(Keys.PAGE_DOWN)
driver.execute_script("arguments[0].scrollBy(0, 10000)", elem)
driver.execute_script('document.getElementsByClassName("section-scrollbox").scrollDown += 1000')

2 Answers 2

1

So basically your requirement is to scroll a particular section of a webpage.

While most of the available answers revolve around scrolling the complete web page itself, it is indeed possible to scroll a particular segment of the webpage entirely.

There are a fair number of sites where the scrollbar is not an identifiable element in source code and therefore clicked to scroll the section down.

What you can do in such a case is to manipulate the section using javascript.

However, if the scrollbar is identified in the source code, configuring a simple click action would get the job done.

Firstly identify the sub-section of the webpage that you want to scroll

xpath_element = "//aside[@class='sidebar mCustomScrollbar_mCS_2']//div[@class='mCSB_container']"

section = driver.find_element(By.XPATH, xpath_element)

Now for an example, let's say you want to scroll the particular section 3 times, therefore we can initialize a counter variable for the same:

counter = 0



while counter < 3:  # this will scroll 3 times
     driver1.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;',
                                 section)
      counter += 1

      # add a timer for the data to fully load once you have scrolled the section

      time.sleep(5) # You might need to install time library to use this statement

And this will help you scroll the section in your webpage. Please note that you can modify the execute_script() as per your requirement, so for example if you wish to scroll to the bottom of the section in each attempt, you can simply use:

driver.execute_script(
    'arguments[0].scrollTop = arguments[0].scrollTop + document.getElementById("search-results-container").scrollHeight;',
    section,
)

where you are getting the scrolling height of section that you were trying to scroll.

Sign up to request clarification or add additional context in comments.

Comments

0
driver.execute_script('document.querySelector(\'[aria-label="Results for Chemists in varanasi district"]\').scrollBy(0,1000)')

you should do scroll on the full scroll bar element

2 Comments

thanks. thats moving the scroller but still didn't solve the issue completely becasue after running the above command new elements are loaded but find elements by class name still returning 7 elements. while(True): driver.execute_script('document.querySelector(\'[aria-label="Results for Chemists in varanasi district"]\').scrollBy(0,1000)') print(len(driver.find_elements_by_class_name("place-result-container-place-link"))) This is always printing 7.
but strangely when i switch to the browser opened by selenium and come back to the jupyter notebook and run print(len(driver.find_elements_by_class_name("place-result-container-place-link"))). then it returns 14 and doing same thign again it returns 20. Not sure why this is happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.