Is it possible to scroll down in a webpage using selenium webdriver programmed on python?

+1 vote
I'm actually using WebDriver to parse through page displaying all names of friends on facebook, and i'm extracting all their ids from the AJAX script. But I'm unable to scroll down to get more friends. How to scroll down using Selenium code? I'm using Python.
May 21, 2018 in Selenium by eLiJha
• 710 points
6,582 views

2 answers to this question.

0 votes

Yup..you can. This is how its done.

driver.execute_script("window.scrollTo(0, Y)") 

where Y is the height (on a fullhd monitor it's 1080).

Or, you can also use this to scroll down to the bottom of the page.

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
answered May 21, 2018 by sniffy_god
• 760 points
Not Working Tell me another command

Hi Shyamal, can you please state what exactly is causing the problem with above command or what is the error you are facing. You can use this line of code also:

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

0 votes
I using next code for facebook for group scraping:

        # Pause time after scrool
        SCROLL_PAUSE_TIME = 2

        # Get page height
        last_height = browser.script(query="return document.body.scrollHeight")

        while True:
            # Make scroll down to bottom
            browser.script(query="window.scrollTo(0, document.body.scrollHeight);")

            # Wait to load page
            time.sleep(SCROLL_PAUSE_TIME)
            # Calculate new scroll height and compare with last scroll height
            new_height = browser.script(query="return document.body.scrollHeight")
            # break by end of results tag (<div class="phm _64f">End of Results</div>)
            try:
                browser.find(method="xpath", query="//div[@class='phm _64f'][contains(.,'End of Results')]", error=0)
            except NoSuchElementException:
                pass
            else:
                break
            # break by size
            if new_height == last_height:
                break
            last_height = new_height
answered May 16 by mslavikas@gmail.com

Hi @Mslavikas, can you suggest any shorter way of doing like this:

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

 As this single line of code can be used to scroll a page down.

Your suggestions are welcomed.

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,070 points
441 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
791 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
433 views
0 votes
2 answers
+2 votes
1 answer

I want the console.log output from Chrome. I'm working with selenium on Python

So this is how you do it ...READ MORE

answered May 3, 2018 in Selenium by sniffy_god
• 760 points
8,986 views